0

I try to send data with ajax to c# method. I getting files from input type=file

var inp = document.getElementById('file');
var data = new FormData();

for (var i = 0; i < inp.files.length; ++i) {
   var somefile  = inp.files.item(i);
   //for is for later not have function right now!
}

$.ajax({
   url: "Main/Upload",
   type: "GET",
   data: {"file":somefile},
});

I write c# method:

public void Upload(HttpPostedFileWrapper file)
{
   ModelState.Clear();
   if (file != null)
   {
     if (file.ContentLength > 0)
     {
       var fileName = Path.GetFileName(file.FileName);
       var saveLocation = Path.Combine(Server.MapPath("~/tmp"), fileName);
       file.SaveAs(saveLocation);
     }
   }           
}

I am not sure that HttpPostedFileWrapper is right option but I try... It is problem to use [httpost] because otter issues.

Now ajax call not call Upload.

I get error NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object.

I try to debug in somefile i get file size,name,type etc.

I need to get file from input without submit, i do that,and that part work, now i just need to save file in save folder, maybei can do only with jquery.

1

1 Answer 1

1

Yes your ajax code is wrong.

The above code can be rewritten as

var inp = document.getElementById('file');
var data = new FormData();
data.append("fileInfo", inp);

/*
for (var i = 0; i < inp.files.length; ++i) {
   var somefile  = inp.files.item(i);
   //for is for later not have function right now!
}*/

$.ajax({
   url: "Main/Upload",
   type: "POST",
   data: data,
   success : function (data) {
      console.log("SUCCESS : ", data);
   },
   error : function () {
    console.log("ERROR");
   }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.