Handler没有从AjaxUpload获取请求的数据

时间:2015-01-22 11:09:56

标签: javascript c# jquery asp.net-ajax ajax-upload

我有一个图片上传代码。我为uploading images without postback使用了 AjaxUpload.js 插件。问题是当我传递硬编码值(无论是查询参数还是data助手)时,我能够在我的处理程序中检索它。但是当我传递一些像$('#some_id').val()这样的通用值时,我总是在我的处理程序中收到空字符串。

SCRIPT

  var upload='';
  upload = new AjaxUpload($('#fileOpenDialog'),
    {
        action:  RootPath + 'GraphicResourceHandlers/FileHandler.ashx',         
        data: { OldGraphicName: $('#hidGraphicName').val() },
        type: 'POST',         
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        autoSubmit: false,         
        onChange: function (file, extension) {

           //some code
        },
        onSubmit: function (file, response) {

            alert("Success.");
        },
        onError: function () {
            alert("Error in upload.");
        },
        onComplete: function (file, response) {

            //some code 
          }
    });


   //upload image 
     $('#btnUpload').click(function () {

        //This will call the AjaxUpload .
        upload.submit();

         //Hides the modal that asks for uploading image
        $('#browseFile').modal('hide');
     });

FileHandler.ashx

   public void ProcessRequest(HttpContext context)
    {
        try
        {
            context.Response.ContentType = " text/html";        

            //This line is returning an empty string         
            context.Request["OldGraphicName"]

           //I also did this while passing as a query paramter but no luck
            context.Request.QueryString["OldGraphicName"]

            string graphicPath = Config.GraphicsPath;
            string locationPath = HttpContext.Current.Request.PhysicalApplicationPath + graphicPath;
            string fileName = context.Request.Files[0].FileName;
            string newFileName = Guid.NewGuid() + Path.GetExtension(fileName);
            context.Request.Files[0].SaveAs(locationPath + newFileName);
            context.Response.Write(newFileName);

        }
    }

有人可以查看这个问题。我真的很努力地弄清楚我的错误。

谢谢!

1 个答案:

答案 0 :(得分:0)

你试过这个吗..

var fileOpenDialog= $("#fileOpenDialog").val(); 

和ajax

data: {OldGraphicName: fileOpenDialog}

抱歉,如果我错了......