Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application i am sending file with html file api to controller...
like..
xhr.setRequestHeader("Cache-Control", "no-cache");
                         xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                         xhr.setRequestHeader("Content-Type", "multipart/form-data");
                         xhr.setRequestHeader("X-File-Name", file.fileName);
                         xhr.setRequestHeader("X-File-Size", file.fileSize);
                         xhr.setRequestHeader("X-File-Type", file.type);
                        xhr.setRequestHeader("X-File", file);

                         // Send the file (doh)
                         xhr.send(file);

and controller side is
[HttpPost]
        public JsonResult Upload(object fileToUpload1)
        {
 var fileName = Request.Headers["X-File-Name"];
            var fileSize = Request.Headers["X-File-Size"];
            var fileType = Request.Headers["X-File-Type"];
            //var x = DateTime.Now.Second;
            //var y = x + 20;
            //if((y-x)>x)
            if (fileName != null)
            {
                
                Request.SaveAs("D:\\uploadimage\\" + fileName, false);
}
  return Json(true, JsonRequestBehavior.AllowGet);
}

It works fine...but i want to cast that object to HttpPostedFileBase type so i can pass to method whose parameter is of type HttpPostedFileBase..I try like below but it gives null value..
HttpPostedFileBase hpf = Request.Files["fileToUpload1"];
Posted

1 solution

hi,
Try this if could help:

C#
try
 {
      Byte[] imgByte = null;
      // Cast object to FileUpload
      FileUpload img = (FileUpload)Upload(fileToUpload1)

      if (img.HasFile && img.PostedFile != null)
      {
          //Create a PostedFile
          HttpPostedFile file = img.PostedFile;
          //Create byte Array with file len
          imgByte = new Byte[file.ContentLength];
          //force the control to load data in array
          file.InputStream.Read(imgByte, 0, file.ContentLength);
          originalSize = file.ContentLength;
      }
  }
  catch
  {
  }
}


Please do not forget to vote if could help so that others may consider as answer...

Regards,
 
Share this answer
 
Comments
vishal_h 15-Dec-11 0:50am    
But what is Fileupload ..And in which namespace is it define ...i am using ASp.net MVC3 with C#...
Al Moje 15-Dec-11 1:06am    
namespace is
System.Web.UI.WebControls

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900