Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,,

Currently I am using below code to upload file with form method = post.

C#
function saveForm(stepTo) {
         nextStep = stepTo;
         $('#wizard').smartWizard('showLoader');
         if ($('#wizard').smartWizard('currentStep') == model.supportingDocPageStepNum) {
             var $ele = $("#step-" + model.supportingDocPageStepNum.toString());
             var fileUploader = new FileUploader($ele, '@(ViewBag.FileUploadAjaxApiUrl)', ajaxSaveForm);
             fileUploader.submit();
         }
         else {
             ajaxSaveForm(null, null);
         }
     }

I an trying to replace fileUpload with Ajax .I have file type in different file and it has no id it is dynamically generating file names..

I am using below code for Ajax but I dont know how to pass file data in below method... I just have file name no file id

C#
var ajaxRequest = $.ajax({
                  type: "POST",
                  url: "/api/fileupload/uploadfile",
                  contentType: false,
                  processData: false,
                  data: data
              });


C#
var data = new FormData();

              var files = $("#fileUpload") just stuck how t0 get fileData ... can I use something like var x = document.getElementsByName("filename").file 

I am getting error
Posted
Updated 18-May-15 0:33am
v2
Comments
Afzaal Ahmad Zeeshan 18-May-15 8:01am    
Hello, you are required to pass the actual HTML form in the FormData to create an object to hold the file of your HTML web page. Please see Solution 2 for more.

FormData can be used to upload file. You can try something like below.


JavaScript
var Ajaxdata = new FormData();
            jQuery.each($('#FileUpload')[0].files, function (i, file) {
                data.append('Filedata', file);
            });



and than send this Ajaxdata in AjaxCall's data portion like.

JavaScript
$.ajax({
                url: 'http://Your URL//Upload.ashx',
                data: Ajaxdata,
              .................
               ..................
   });


and finally in handler or controller get the posted file as,

 
C#
HttpPostedFile postedFile = context.Request.Files["Filedata"];



Hope it will work. :)
 
Share this answer
 
v2
The jQuery file upload method is a very simple and yet easy method for uploading the files over HTTP. jQuery provides you with built in functions and objects that can be used to encode and transmit the files to the server... using ajax!

First of all, you are required to have a FormData object, which holds the data of the form and thus the file. You can create so using,

JavaScript
var formData = new FormData($('#form')[0]);


That is not enough. You are required to tune a few other things up before launching the request.

JavaScript
type: 'POST',
processData: false,
contentType: false,
data: formData,


The type of HTTP should be POST, after that you should set these other two properties to false. Data object is the data that would be passed along with the request. This would hold the FormData object. Now, when the request would be made (to current page if url is not provided) the file would be sent along with the request. You can access the files in a way that you would in your own server-side language. In ASP.NET you would write the following code,

C#
files = Request.Files.Count;
if(files > 0) {
   // Files are sent
   for (int i = 0; i < files; i++) {
      var file = Request.Files[i];
      // Got the image...
      string fileName = Path.GetFileName(file.FileName);
      // Save the file...
      file.SaveAs(Server.MapPath("~/" + fileName));
   }
}


For more please read this tip of mine: Uploading the files – HTML5 and jQuery way![^]
 
Share this answer
 

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