c# - 提交到ashx时jquery文件上传错误

标签 c# jquery file-upload httphandler

我正在尝试使用 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 + "\"}");

您传回的对象只是格式错误。这应该可以解决问题。

关于c# - 提交到ashx时jquery文件上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5081119/

相关文章:

java - 移动网络应用程序的表单字段中的文件上传

jquery - 如何使用 JQuery 在输入类型文件中验证文件扩展名?

时间:2019-03-17 标签:c#app: Is it possible to implement a JSON interface?

c# - c#中用于查找阶乘的数据类型

javascript - 为什么将内容插入选项卡后连接线会消失?

javascript - 使用 Jquery/Javascript 根据下拉选项删除元素

ruby-on-rails - 如何使用carrierwave为第一个pdf页面创建缩略图

c# - 使用反射的内部构造函数实例化类型

c# - 替换字符串 C# 中的换行符

javascript - 对两个按钮使用 toggleClass - $(this) jquery 对象的问题