not able to upload file using Ajax.BeginForm()

sam
sam
Member
378 Points
48 Posts

Hi,

I am trying to upload file in Ajax.BeginForm() as:

In View:

@using (Ajax.BeginForm("UploadFile","File", null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "result" }, new { enctype = "multipart/form-data" }))
{
<label id="lblUploadNewFile" for="fileUploadControl"> Upload New File </label>
<input type="file" name="fileToUpload" id="fileUploadControl" />
<input id="btnFileUpload" type="submit" value="Upload" class="buttongreen" />
<span id="result" />
}

 In controller:

[HttpPost]
public string UploadFile(FormCollection formData)
{
HttpPostedFileBase file = null;
try
{
file = Request.Files[0];
}
catch { }
if (file != null && file.ContentLength != 0)
{
file.SaveAs(string.Concat(
AppDomain.CurrentDomain.BaseDirectory,
System.IO.Path.GetFileName(file.FileName)));
return "Successfully Uploaded";
}
else
{
return " Failed, please try again.";
}
}

 

I am getting failed.

Please help.

Views: 11970
Total Answered: 1
Total Marked As Answer: 0
Posted On: 03-Nov-2015 04:58

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hi Sam,

Ajax.Beginform() does not support for file upload. To upload file you need to use Html.Beginform() or you can use jquery ajax file uploader.

Posted On: 18-Nov-2015 05:58
 Log In to Chat