Hi,
I did not get to uploading files in asp.net mvc myself
yet, but I recalled that I went over the topic earlier this weekend
when working my way through a book. Not sure this will answer your
question, but here's a bit of text I just copied straight from the
book.
(It's a little noobish, but I'm not sure where is your problem)
Creating an upload form
When creating an upload
form, one should always include a special HTML attribute in the form
tag, specifying the encoding type as multipart/form-data. This
directive tells a browser to post the HTML form back to the server in
multiple
parts: one part for regular form fields, and another for each file.
<% using (Html.BeginForm("Upload", "Home", FormMethod.Post, new
{
enctype = "multipart/form-data"
}))
{ %>
File 1:
<input type="file" name="file1" id="file1" /><br />
File 2:
<input type="file" name="file2" id="file2" />
<input type="submit" id="upload" value="Upload" />
<% } %>
Creating an upload controller action
Handling an
upload form is quite easy. The HttpRequest object provides property
Files that contain a set of HttpPostedFileBase instances.These contain
all sorts of information for a specific file that is being uploaded:
content length, content type, and the filename on the user's computer.
The SaveAs() method allows you to save the uploaded file somewhere on
the web server or to a connected network directory.
public ActionResult Upload()
{
StringBuilder info = new StringBuilder();
foreach (string file in Request.Files)
{
HttpPostedFileBase postedFile = Request.Files[file];
if (postedFile.ContentLength == 0)
continue;
/* The following would save the file on the server:
* string newFileNameOnServer = Path.Combine(
* AppDomain.CurrentDomain.BaseDirectory,
* Path.GetFileName(postedFile.FileName));
* postedFile.SaveAs(newFileNameOnServer);
*/
info.AppendFormat("Uploaded file: {0}\r\n",postedFile.FileName);
}
if (info.Length > 0)
{
ViewData["Info"] = info.ToString();
}
return View("UploadForm");
}