我正在尝试使用 Jquery file upload将文件异步上传到 C3 http 处理程序的插件。我已经完成了 GitHub site 上的设置步骤为项目。它似乎在 Firefox 中运行良好,但在 IE 中抛出 javascript 错误(“异常抛出且未被捕获”。第 95 行,字符 25,文件:test.html),即使文件已成功上传。我认为我的问题与我的 ashx 的响应有关。谁能看出我做错了什么?
这是我页面的 html 正文 (test.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;
}
}
}
}
尝试改变:
context.Response.Write("{\"name\":\"" + fileupload.FileName + "\",\"type\":\"" + fileupload.ContentType + "\",\"size\":\"" + fileupload.ContentLength + "\"}");
对此:
context.Response.Write("{\"name\":\"" + fileupload.FileName + "\", type:\"" + fileupload.ContentType + "\",size:\"" + fileupload.ContentLength + "\"}");
您传回的对象只是格式错误。这应该可以解决问题。
我是一名优秀的程序员,十分优秀!