public ActionResult FileUpload()
{
string relativePath = string.Empty;
if (file != null)
{
// string ImageUniqueName = Guid.NewGuid().ToString();
//string ActualImageName = ImageUniqueName + Path.GetFileName(file.FileName);
string pic = System.IO.Path.GetFileName(file.FileName);
path = System.IO.Path.Combine(Server.MapPath("/images/profile"), pic);
// file is uploaded
file.SaveAs(path);
relativePath = Path.Combine("/images/profile", ActualImageName);
}
return Json(relativePath, JsonRequestBehavior.AllowGet);
}Loading
Vishal JoshiPosted Jan 23, 2024, 10:29 AM
Hello, Emmanuel ,
Please check you ajax call for request for the API.
Remove Type as json.
Also In parameter it requires file parameter of type HttpPostedFileBase
so it should be like below
public ActionResult FileUpload(HttpPostedFileBase file){}
Thanks
Vishal Joshi
Emmmanuel FIADUFEPosted Jan 24, 2024, 11:44 AM
Thank you team for your support
Emmmanuel FIADUFEPosted Jan 22, 2024, 11:52 AM
Thank you Naimish, please I will check and revert
Naimish MakwanaPosted Jan 22, 2024, 8:44 AM
There are a few issues with your code. Here’s a corrected version:
In this corrected version:
fileis of typeHttpPostedFileBasewhich represents an uploaded file.file.SaveAs(path).Please make sure to replace
"~/uploads/"with the actual path where you want to save the files on your server. Also, ensure that the server has the necessary write permissions to that directory.Remember, this is a basic example and doesn’t include error handling and file validation (like checking file size, file type etc.) which you should implement in a production environment.
Thanks