Answered by:
Is there a way to upload images outside of my project in ASP.NET MVC 5?

Question
-
User-1634604574 posted
Is there a way to upload images outside of my project in ASP.NET MVC 5?without entity frame work i need by ajax
upload image into local c:\temp\
i have this code but i don't know how to write controller and view for it
<appSettings> <add key="DocumentationLocation" value="E:\img\imgProfile" /> </appSettings>
string savePath = ConfigurationSettings.AppSettings["DocumentationLocation"]; string filename = Path.GetFileName(FileUploadControl.FileName); FileUploadControl.SaveAs(savePath + filename);
Wednesday, October 9, 2019 9:46 AM
Answers
-
User61956409 posted
Hi zhyanadil.it@gmail.com,
zhyanadil.it@gmail.com
Is there a way to upload images outside of my project in ASP.NET MVC 5?without entity frame work i need by ajaxTo achieve the above requirement, please refer to the following code snippet.
In Controller
public ActionResult UploadFiles() { try { HttpFileCollectionBase files = Request.Files; if (files.Count > 0) { for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; string fname; fname = Path.GetFileName(file.FileName); string savePath = ConfigurationSettings.AppSettings["DocumentationLocation"]; fname = Path.Combine(savePath, fname); file.SaveAs(fname); } return Json("File Uploaded"); } else { return Json("Please Select File"); } } catch (Exception ex) { return Json("Upload Failed. Error: " + ex.Message); } }
In View
<h2>File Upload</h2> <input id="FileUploader" type="file" multiple /> <input id="btn_upload" type="button" value="Upload" /> @section scripts{ <script> $("#btn_upload").click(function () { var files = $("#FileUploader").get(0).files; var fileData = new FormData(); for (var i = 0; i < files.length; i++) { fileData.append(files[i].name, files[i]); } $.ajax({ url: '/Home/UploadFiles', type: "POST", contentType: false, processData: false, data: fileData, success: function (res) { console.log(res); }, error: function (err) { console.log(err); } }); }) </script> }
Besides, as rymling mentioned, please make sure it has permissions to access and operate the folder on server that you specified for storing files.
With Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 10, 2019 5:40 AM
All replies
-
User550915133 posted
You need to make sure the account that the application pool is using in IIS has been granted permissions to that path on the server (c.\temp or E:\img\imgProfile, which is it?)
Don't know what EF or AJAX has anything to do with this.
Wednesday, October 9, 2019 5:40 PM -
User61956409 posted
Hi zhyanadil.it@gmail.com,
zhyanadil.it@gmail.com
Is there a way to upload images outside of my project in ASP.NET MVC 5?without entity frame work i need by ajaxTo achieve the above requirement, please refer to the following code snippet.
In Controller
public ActionResult UploadFiles() { try { HttpFileCollectionBase files = Request.Files; if (files.Count > 0) { for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; string fname; fname = Path.GetFileName(file.FileName); string savePath = ConfigurationSettings.AppSettings["DocumentationLocation"]; fname = Path.Combine(savePath, fname); file.SaveAs(fname); } return Json("File Uploaded"); } else { return Json("Please Select File"); } } catch (Exception ex) { return Json("Upload Failed. Error: " + ex.Message); } }
In View
<h2>File Upload</h2> <input id="FileUploader" type="file" multiple /> <input id="btn_upload" type="button" value="Upload" /> @section scripts{ <script> $("#btn_upload").click(function () { var files = $("#FileUploader").get(0).files; var fileData = new FormData(); for (var i = 0; i < files.length; i++) { fileData.append(files[i].name, files[i]); } $.ajax({ url: '/Home/UploadFiles', type: "POST", contentType: false, processData: false, data: fileData, success: function (res) { console.log(res); }, error: function (err) { console.log(err); } }); }) </script> }
Besides, as rymling mentioned, please make sure it has permissions to access and operate the folder on server that you specified for storing files.
With Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 10, 2019 5:40 AM