I am using MVC3 web application with razor view. I have to upload all the files listed in the csv file. CSV file having file name and file path.
All the file list will be stored in MySql and all the images has to be uploaded in the server.
Please find the steps i am following...
I have iterated over csv and i was able to add the list of paths to the db..
here i don't have file upload control in the form. so i am not able to use the below code to upload the file.
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
foreach (var file in files) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
So i have used the below code to upload the file. for instance, i have hardcoded the file path.
FileInfo fi = new FileInfo("C:\\Test.jpg");
long nFileLen = fi.Length;
FileStream rFile = new FileStream("C:\\Test.jpg", FileMode.Open);
byte[] myData = new byte[nFileLen];
rFile.Read(myData, 0, (int)nFileLen);
It is having exception that file not found. because it was searching in the server memory.
Please help me to find the way.