使用ashx filehandler和ajax上传文件总是返回错误

时间:2015-05-25 11:35:12

标签: jquery .net file-upload webforms ashx

我正在使用blueimp(https://blueimp.github.io/jQuery-File-Upload/)的jquery-file-upload。

我有FileHandler.ashx用于文件上传。

我无法从文件处理程序返回正确的响应。在JavaScript方面,它总是收到错误。请指导我。

//文件处理程序(FileHandler.ashx)

$(function () {
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = 'FileHandler.ashx';
    $('#fileupload').fileupload({
        url: url,
        dataType: 'json',
        done: function (e, data) {

        },
        fail: function (e, data) {

        },
    });
});

// javascript part:

SyntaxError: Unexpected token C

resut总是"失败"从来没有"成功"。错误是CREATE PROCEDURE orders.InsertOrder( ... ) AS BEGIN DECLARE @OrderId INT BEGIN TRANSACTION SELECT @OrderId = MAX(OrderId) + 1 FROM orders.Orders WITH (TABLOCK, HOLDLOCK) INSERT INTO [orders].[Orders] ([OrderId], ... ) VALUES (@OrderId, ... ); COMMIT TRANSACTION END 。 这在某种程度上与我返回的响应有关,它的格式不正确,但它应该采用什么格式?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

  public class ResponseMsg
    {
        public string status;
        public string name;
    }
public void ProcessRequest(HttpContext context)
    {
        string fname = String.Empty;
        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection files = context.Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];                    
                fname = context.Server.MapPath("~/UserImageUploads/" + file.FileName);
                file.SaveAs(fname);
            }
        }
        context.Response.ContentType = "text/plain";
       // context.Response.Write(fname);
      //  context.Response.StatusCode = 200;

        ResponseMsg r = new ResponseMsg
        {
            status = "success",
            name =fname
        };
       context.Response.Write(JsonConvert.SerializeObject(r));
    }