views:

1082

answers:

2

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!

+4  A: 

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" })) { %> 

<% } %>
Brehtt
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?
Giovanni Galbo
instead of Request.Files[0] you should be able to use Request.Files["formInputname"]
Brehtt
Brehtt
Ahhhh... I guess i'm a dummy. I was trying Form["formInputName"]. Thanks!
Giovanni Galbo
thank you sir, really helpful answer and comments.
Konstantinos
A: 

if i do the same using Castle Mono Rail i will get the same results? i put Request.Files but i have issues, maybe its bc the framework i'm working with .Net Framework 2.0 and i coudnt get multiple files... i also tryed with HttpFileCollection without good results :(

Mau