2

アップロード jquery コントロールを使用しようとしています。以下のコードは、ASP.Net Handler に正常に接続し、ファイルを正常に処理します。処理後、データの文字列をクライアントに送り返す必要があります。

これが私が取り組んでいる例です..

私はいくつかの問題を抱えています..

1) Handler からクライアントにデータを送り返す方法

2) ハンドラーの成功のコードをキャッチする場所を理解している人はいますか。購読するためのOnComplete、doneメソッドも表示されません。

これが私のハンドラーです..

  public class FileUpload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            if (context.Request.Files.Count == 0)
            {

                LogRequest("No files sent.");

                context.Response.ContentType = "text/plain";
                context.Response.Write("No files received.");

            }
            else
            {

                HttpPostedFile uploadedfile = context.Request.Files[0];

                string FileName = uploadedfile.FileName;
                string FileType = uploadedfile.ContentType;
                int FileSize = uploadedfile.ContentLength;

                LogRequest(FileName + ", " + FileType + ", " + FileSize);

                string theName=uploadedfile.FileName.Substring(uploadedfile.FileName.LastIndexOf('\\'));

                uploadedfile.SaveAs(HttpContext.Current.Server.MapPath("/Upload") + theName);

                context.Response.ContentType = "text/plain";
                context.Response.Write("{\"name\":\"" + FileName + "\",\"type\":\"" + FileType + "\",\"size\":\"" + FileSize + "\"}");

                context.Response.Write("Hi From Handler");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

これが私のクライアントコードです..

 <script>
            /*global $ */
            $(function () {
                $('#file_upload').fileUploadUI({
                    url: 'FileUpload.ashx',
                    method: 'POST',
                    uploadTable: $('#files'),
                    downloadTable: $('#files'),
                    buildUploadRow: function (files, index) {
                        return $('<tr><td>' + files[index].name + '<\/td>' +
                            '<td class="file_upload_progress"><div><\/div><\/td>' +
                            '<td class="file_upload_cancel">' +
                            '<button class="ui-state-default ui-corner-all" title="Cancel">' +
                            '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                            '<\/button><\/td><\/tr>');
                    },
                    buildDownloadRow: function (file) {
                        return $('<tr><td>' + file.name + '<\/td><\/tr>');
                    },
                    parseResponse: function (data) {
                        alert("yo");
                    }
                });    
            });

        </script> 
4

3 に答える 3

1

1) Handler からクライアントにデータを送り返す方法

あなたのやり方は正しいようです。応答ストリームに書き出すだけです。

2) ハンドラーの成功のコードをキャッチする場所を理解している人はいますか。購読するためのOnComplete、doneメソッドも表示されません。

私はfileUploadUIプラグインを使用していませんが、ドキュメントに基づいて、sendメソッドはjqXHRオブジェクトを返します。これをsuccess関数にバインドできるはずです

これがドキュメントのスニペットです

var jqXHR = $('#fileupload').fileupload('send', {files: filesList})
    .success(function (result, textStatus, jqXHR) {/* ... */})
    .error(function (jqXHR, textStatus, errorThrown) {/* ... */})
    .complete(function (result, textStatus, jqXHR) {/* ... */});

編集:
コールバックにバインドする方法は 2 つあります。オプション オブジェクトを使用してコールバックを渡す方法と、後でイベント リスナーをウィジェット要素にバインドする方法です。

例えば:

 $('#fileupload').fileupload({
         always: function (e, data) {
           //the jqXHR is a property of the data object        
         },
     //rest of the options
   });
于 2012-12-21T17:44:58.257 に答える
1

Jack の応答の一部に追加します。イベント コールバックは次の 2 つの方法で定義できます。

1) イベントリスナーをウィジェット要素にバインドします。

$('#fileupload')
    .bind('fileuploadsend', function (e, data) {/* ... */})
    .bind('fileuploaddone', function (e, data) {/* ... */})
    .bind('fileuploadfail', function (e, data) {/* ... */})

これらの関数は、ウィジェットのコールバックが実行された後に呼び出されます。したがって、「fileuploaddone」を使用してバインドされた関数は、ウィジェットの「done」コールバックが実行された後に呼び出されます。

2) ウィジェットのコールバックを完全にオーバーライドするには、ウィジェットのオプションの一部としてコールバックを定義できます。

$('#fileupload').fileupload({
     done: function (e, data) {

     },
     send: function (e, data) {

     },
     //other options

});

すべてのウィジェット イベントのドキュメントはこちら: https://github.com/blueimp/jQuery-File-Upload/wiki/Options (「コールバック オプション」を検索)

IE9 で完了またはその他のコールバックで問題が発生した場合は、ここでの応答が役立つ可能性があります: blueimp jquery file upload - "done", "complete" callbacks not working for IE 9

お役に立てれば。

于 2012-12-22T20:03:29.610 に答える