1

I have the following function:

public class UserRequest
{    
    public string RequestMessage { get; set; }
    public HttpPostedFileBase Photo { get; set; }
}        
public void CreateRequest(UserRequest userRequest)
{
}

When I post to this function with the following script the posted image is always null:

var requestMessage = document.getElementById("txtReqMessage").value;
var inputFile = document.getElementById("reqphoto");
var imageFile = inputFile.files[0];

$.ajax({
     type: "POST",
     url: "/UserRequest/CreateRequest/",
     contentType: 'application/json; charset=utf-8',
     data: JSON.stringify({                      
        "RequestMessage": requestMessage,
        "Photo": imageFile
     }),
    success: function () {
        alert("Success");
} 

I read about posting complex data so I changed the script to:

var requestMessage = document.getElementById("txtReqMessage").value;
var inputFile = document.getElementById("reqphoto");
var imageFile = inputFile.files[0];    

var formData = new FormData();
formData.append("RequestMessage", requestMessage);
formData.append("Photo", imageFile);   

    $.ajax({
       type: "POST",
       url: "/UserRequest/CreateRequest/",
       data: formData,
       dataType: 'json',
       contentType: false,
       processData: false,
       success: function () {                  
           alert("Success");
       }
});

But now something is wrong I get Error 500.

2
  • 500 error means server error, so try to debug your server code Commented Dec 17, 2014 at 10:45
  • This is an old thread, but I have to say it - the extra slash at the end of the URL is causing the 500 error. Commented Jan 27, 2016 at 15:55

3 Answers 3

3

I changed it to this:

public void CreateRequest()
        {

            var httpRequest = HttpContext.Current.Request;

            var userRequest = new UserRequest
            {                
                RequestMessage = httpRequest.Form["RequestMessage"],
                Photo = httpRequest.Files["RequestPhoto"],

            };                                               
        }



var formData = new FormData();            
formData.append("RequestMessage", requestMessage);
formData.append("RequestPhoto", imageFile);


$.ajax({
   type: "POST",
   url: "/UserRequest/CreateRequest",
   data: formData,      
   processData: false,
   success: function () {
    alert("OK");
   },
   error: function () {
    alert("Error");
 }
 });    
Sign up to request clarification or add additional context in comments.

Comments

0

define the method as web method

[WebMethod]
public void CreateRequest(UserRequest userRequest)
{

}

Comments

0

you should read data in a FormDataCollection

public void CreateRequest(FormDataCollection form)
{

}

try changing contentType ajax property to

contentType: 'multipart/form-data'

you are using false value that should be used only when you are sending files

2 Comments

tryied with both scripts: it's empty.
try with contentType: 'multipart/form-data', you are using false that should be used only when you send files

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.