【问题标题】:Handler is not getting the requested data from AjaxUpload处理程序未从 AjaxUpload 获取请求的数据
【发布时间】:2015-01-22 11:09:56
【问题描述】:

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

脚本

  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);

        }
    }

有人可以调查一下这个问题吗?我真的很努力地找出我的问题所在。

谢谢!

【问题讨论】:

  • 我可以通过这个 script 在服务器上上传具有唯一名称的图像。现在我需要用旧文件重命名新上传的文件。为此,我将旧图像名称保存在隐藏字段中,并将其作为参数通过 AjaxUpload 传递给处理程序。
  • @Pete ??我在添加我的评论时忘记引用你了

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


【解决方案1】:

你试过了吗..

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

在 ajax 中

data: {OldGraphicName: fileOpenDialog}

对不起,如果我错了......

【讨论】:

  • 你没有正确看到我的代码。我没有将fileOpenDialog 值传递给处理程序,而是传递了一个隐藏字段值$('#hidGraphicName').val() 。这不是上传图片的值,而是服务器上已经存在的图片的值。我想用这个值更改上传图片的名称,即$('#hidGraphicName').val()
猜你喜欢
  • 2017-07-29
  • 2021-09-18
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
相关资源
最近更新 更多