5

I'm very new to ASP.net MVC, so please be as descriptive as possible in your answer :)

Let me simplify what I'm trying to do. Imagine I have a form where you want to enter some information about a car. The fields might be: Make, Model, Year, Image1, Image2.

On the bottom of the form is a "Save" button. The associated Controller method will save Image1 and Image2 to disk, obtain their filenames and associate them with the car model, which will then be saved to the database.

Any ideas?

Thanks guys!

Edit

winob0t got me most of the way there. The only outstanding issue is the following: Image1 and Image2 are not required fields, so I now I can save 0,1 or 2 images; but if the user only uploads 1 picture I have no way of knowing if it came from imageUpload1 or imageUpload2.

Again, any help is appreciated!

2 Answers 2

7

In your controller you can access the uploaded files as:

    if(Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) {
        HttpPostedFileBase postFile = Request.Files.Get(0);
        string filename = GenerateUniqueFileName(postFile.FileName);
        postFile.SaveAs(server.MapPath(FileDirectoryPath + filename));
    }

protected virtual string GenerateUniqueFileName(string filename) {

    // get the extension
    string ext = Path.GetExtension(filename);
    string newFileName = "";

    // generate filename, until it's a unique filename
    bool unique = false;

    do {
        Random r = new Random();
        newFileName = Path.GetFileNameWithoutExtension(filename) + "_" + r.Next().ToString() + ext;
        unique = !File.Exists(FileDirectoryPath + newFileName);
    } while(!unique);
    return newFileName;
}

The text fields will arrive at your controller action as per usual i.e. Request.Form[...]. Note that you will also need to set the enctype on the form to "multipart/form-data". It sounds like you understand enough about ASP.NET MVC to do the rest. Note also that you can declare your form tag in the aspx view as follows, though you can use the more traditional approach if you like.

<% using(Html.BeginForm<FooController>(c => c.Submit(), FormMethod.Post, new { enctype = "multipart/form-data", @id = formId, @class = "submitItem" })) { %> 

<% } %>
Sign up to request clarification or add additional context in comments.

4 Comments

You got me half way there! Only one slight issue: Image1 and Image2 are not guaranteed to be there. So what if the user only supplies Image2 and not Image1? Is there a way to know which upload control it came from?
instead of Request.Files[0] you should be able to use Request.Files["formInputname"]
if(Request.Files["formInputname"] != null && Request.Files["formInputname"].ContentLength > 0)
Ahhhh... I guess i'm a dummy. I was trying Form["formInputName"]. Thanks!
1

Here is my solution, the above answer didn't quite work for my situation. It cares nothing about the form specifics and will allow multiple uploads.

    for (int i = (Request.Files.Count - 1); i >= 0; i--)
    {
          if (Request.Files != null && Request.Files[i].ContentLength > 0)
          {
               string path = this.Server.MapPath("~/Content/images/");
               string filename = Path.GetFileName(Request.Files[i].FileName);
               string fullpath = Path.Combine(path, filename);
               Request.Files[i].SaveAs(fullpath);
           }
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.