0

I have a code for image upload . I used AjaxUpload.js plugin for uploading images without postback . The problem is when i am passing hardcoded values (whether as query paramter or by data helper) , I am able to retrieve that in my handler . But when i am passing some generic value like $('#some_id').val() , then i always recieve empty string in my handler .

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

        }
    }

Can someone please look into this issue . I really toiled hard to figure out what is wrong on my part .

Thanks !

Tushar Raj
  • 761
  • 6
  • 20
  • I am able to upload images on server by this `script` with unique names . The thing is now i need to rename the newly uploaded file with the old one .For that i am keeping the old image name in a hidden field and passing it as a paramter through AjaxUpload to the handler . – Tushar Raj Jan 22 '15 at 11:26
  • @Pete ?? I forgot to reference you while adding my comment – Tushar Raj Jan 22 '15 at 11:33

1 Answers1

0

Have you tried this..

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

and in ajax

data: {OldGraphicName: fileOpenDialog}

sorry if i was wrong....

Saravana
  • 33
  • 1
  • 2
  • 8
  • you didn't see my code properly . I am not passing `fileOpenDialog` value to the handler , rather i am passing a hidden field value `$('#hidGraphicName').val() ` . This is not the value of the uploading image but the value of the image that already exists on the server . I want to change the name of the uploading image with this value i.e. `$('#hidGraphicName').val() ` – Tushar Raj Jan 22 '15 at 11:45