提交到ashx时,Jquery文件上传错误

时间:2011-02-22 16:44:55

标签: c# jquery file-upload httphandler

我正在尝试使用Jquery file upload插件将文件异步上传到C3 http处理程序。我已经完成了项目GitHub site的设置步骤。它似乎在Firefox中正常工作,但在IE中抛出了一个javascript错误('异常抛出并未被捕获'。第95行,字符25,文件:test.html),即使文件已成功上传。我认为我的问题与我的ashx的反应有关。谁能看到我做错了什么?

以下是我的网页(test.html)的html正文:

<form id="file_upload" action="testupload.ashx" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" multiple>
    <button>Upload</button>
    <div>Upload files</div>
</form>
<table id="files"></table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script src="../Script/jquery.fileupload.js"></script>
<script src="../Script/jquery.fileupload-ui.js"></script>
<script>
/*global $ */
$(function () {
    $('#file_upload').fileUploadUI({
        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>');
        }
    });
});
</script>

以下是我的ashx中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;


namespace Testing
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    public class TestUpload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpPostedFile fileupload = context.Request.Files[0];


            string strFileName = Path.GetFileName(fileupload.FileName);
            string strExtension = Path.GetExtension(fileupload.FileName).ToLower();
            string strSaveLocation = context.Server.MapPath("Upload") + "\\" + strFileName;
            fileupload.SaveAs(strSaveLocation);

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

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

尝试更改:

context.Response.Write("{\"name\":\"" + fileupload.FileName + "\",\"type\":\"" + fileupload.ContentType + "\",\"size\":\"" + fileupload.ContentLength + "\"}");

对此:

context.Response.Write("{\"name\":\"" + fileupload.FileName + "\", type:\"" + fileupload.ContentType + "\",size:\"" + fileupload.ContentLength + "\"}");

你传回来的对象只是格格不入。这应该可以解决问题。

答案 1 :(得分:0)

我能够让这个工作......

首先,我尝试对名称,类型和大小响应使用硬编码值,并发现它有效。这是我使用的:

context.Response.Write("{\"name\":\"test\",\"type\":\"test\",\"size\":\"12345\"}");

然后我注意到fileupload.FileName给出了文件的完整路径并假设这是问题所在。当我在response.write行中将fileupload.FileName更改为strFileName时,它在Firefox和IE中都能成功运行。这是一条有用的线:

context.Response.Write("{\"name\":\"" + strFileName + "\",\"type\":\"" + fileupload.ContentType + "\",\"size\":\"" + fileupload.ContentLength + "\"}");