【问题标题】:Jquery File upload error when submitting to ashx提交到ashx时Jquery文件上传错误
【发布时间】:2011-07-02 03:41:55
【问题描述】:

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

【问题讨论】:

  • 哎呀...JS 错误是“抛出异常但未捕获”。第 95 行,字符 25。我会更新我的帖子。

标签: c# jquery file-upload httphandler


【解决方案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 + "\"}");

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

【讨论】:

  • 感谢您的回复。我尝试了该更改,但无法修复错误。它还破坏了 Firefox。
  • 这是一个非常无用的 IE javascript 错误 - 我想我将其追溯到 jquery.min 中的一行。与此同时,我尝试了一下 ashx 的响应并找到了解决方案。我会发布它作为答案。感谢您的帮助!
【解决方案2】:

我能够得到这个工作......

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

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 + "\"}");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多