简体   繁体   中英

Jquery File upload error when submitting to ashx

I am attempting to use the Jquery file upload addon to asynchronously upload files to a C3 http handler. I have gone through the setup steps on the GitHub site for the project. It seems to work fine in Firefox but throws a javascript error in IE ('Exception thrown and not caught'. Line 95, Char 25, File: test.html), even though the file is successfully uploaded. I think my issue is related to the response of my ashx. Can anyone see what I am doing wrong?

Here is the body of the html for my page (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>

Here is the code in my 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;
            }
        }
    }
}

Try changing:

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

To this:

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

Your object that you were passing back was just malformed. That should do the trick.

I was able to get this working...

First, I tried using hardcoded values for the name, type, and size responses and found it worked. Here is what I used:

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

I then noticed that fileupload.FileName was giving the full path of the file and assumed that was the problem. When I change fileupload.FileName to strFileName in my response.write line, it works successfully in both Firefox and IE. Here is a line that works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM