-1

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.

user2516064
  • 11
  • 1
  • 4

2 Answers2

1

It's better to break the problem into smaller chunks, you'll get a more responses that way. As it is, the question is too vague, with no specifics.

You'll need to read the csv file first (you don't say if it needs to be uploaded to the server first - I imagine it would)

Then you'll need to iterate over the csv, locate the listed files and upload them to the server, where your controller code will do the processing & saving to db.

Here are a couple of links to get you started:

MVC 4 Razor File Upload

http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/

Update:

Based on the extra detail you provided, I don't think it's possible to do it the way you're suggesting because the code won't have access to the user's file system.
You may be able to add the filepaths to a model which then populates file inputs in a view, but you'd still need to click the submit button somehow - maybe this could be done using javascript?

Community
  • 1
  • 1
markpsmith
  • 4,860
  • 2
  • 33
  • 62
  • ya you are right. finally i have changed the plan and convinced our client to use file upload control with multiple selection. Thanks for your great support. – user2516064 Feb 05 '14 at 12:00
-1

The best blog to explain about FileUpload in MVC Here

This may not contain the exact solution for your problem. But I hope this will help you to certain extent.

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59