我正在尝试使用上传 jquery 控件。下面的代码成功连接到我的 ASP.Net 处理程序并正常处理文件。处理后,我需要将一串数据发送回客户端。
这是我正在处理的示例..
我有一些问题..
1)如何从处理程序将数据发送回客户端
2) 是否有熟悉的人了解在哪里可以捕获 Handlers 成功的代码。我也没有看到一个 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>