0

I am developing an mvc application and want to upload file into server. At the controller when event fires the fileupload is null.And also the model.fileupload has the path. Actually am submitting my form using ajax post method.

cshtml code....

<form id="frmRegistrationDetails" method="post" enctype="multipart/form-data">
    <table>
      <tr>
         <td>
            <label>Upload File</label><input type="file" id="fileupload"  name="fileupload" />
         </td>
      </tr>
   </table>
</form>

controller code

 [HttpPost]
 public JsonResult SaveDetails(reg_up_mvc model,HttpPostedFileBase fileupload)
  {
  try
  {
    detailsRegistration registrationContext = new detailsRegistration();
    reg_up_mvc registrationDetails = new reg_up_mvc();
    registrationDetails.full_name = model.full_name;
    registrationDetails.fileupload = model.fileupload;
if (model.fileupload != null)
{
string sPath = Path.Combine(Server.MapPath("~/UploadedFilesFromRegistration"), fileupload.FileName);
fileupload.SaveAs(sPath);
registrationDetails.fileupload = sPath;
}

jquery

var data = {
        model: {
                fileupload: $('#fileupload').val()
               }
    };
     $.ajax({
        type: "post",
        data: JSON.stringify(data),
        url: "/Controller/ActionName",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
                           alert("successful")
                           }
         });
  • Please `console.log` the `JSON.stringify(data),` and tell us what it is. – mjwills Sep 13 '19 at 05:29
  • model:fileupload:"C\fakepath\download.jpg".forgot to add id to – Roshan K S Sep 13 '19 at 05:41
  • had tried it by making it same but helpless.so just changed its name field and tried to retrive value by name element.but still hopeless – Roshan K S Sep 13 '19 at 05:56
  • `model:fileupload:"C\fakepath\download.jpg"` That doesn't look like JSON. You are sure it was **exactly** that? – mjwills Sep 13 '19 at 06:04
  • Possible duplicate of [HttpPostedfileBase is null using jQuery Ajax](https://stackoverflow.com/questions/28373894/httppostedfilebase-is-null-using-jquery-ajax) – Rahul Sharma Sep 13 '19 at 06:11
  • `{"model":{"full_name":"sdfdsf","emailid":"sdfdsf","password":"sdfdsfs","state_res":"1","district":"1","gender":"Male","fileupload":"C:\\fakepath\\download-hill.jfif","language":"english"}}` – Roshan K S Sep 13 '19 at 06:16
  • 1
    `,"fileupload":"C:\\fakepath\\download-hill.jfif"` That isn't the file **contents**. That is the **path** to the file. The server can't do anything useful with that. – mjwills Sep 13 '19 at 07:29

1 Answers1

0
https://stackoverflow.com/questions/28373894/httppostedfilebase-is-null-using-jquery-ajax?noredirect=1&lq=1

already answered in this oneplease check

amaldec23
  • 245
  • 2
  • 11