File Upload

Last post 03-29-2009 10:23 AM by niconel. 6 replies.

Sort Posts:

  • File Upload

    03-28-2009, 3:49 PM
    • Member
      20 point Member
    • cerkit
    • Member since 10-17-2003, 8:47 PM
    • Atlanta, GA
    • Posts 10

    I've searched the forums for this but I can't find anything related to MVC.

    Has anyone done any File uploading with MVC?

    Here's my markup:

        Upload Sound: <asp:FileUpload ID="fuSoundFile" runat="server" />

    In the controller, I've included

    using System.Web.UI.HtmlControls;

    And created an instance of HtmlInputFile, but, when I try to work with it on the controller, FormCollection only returns strings. I can't do anything with a string.

    Any thoughts, experience?

    Michael Earls
    http://www.cerkit.com/
  • Re: File Upload

    03-28-2009, 4:03 PM
    • Contributor
      6,711 point Contributor
    • gerrylowry
    • Member since 07-03-2008, 1:46 AM
    • alliston ontario canada
    • Posts 2,281

    check this:  http://forums.asp.net/t/1237419.aspx
                      "howto upload file with mvc"

    Regards,
    Gerry (Lowry)

    Gerry Lowry, Principal
    Ability Business Computer Services ~~ Because it's your Business, our Experience Counts!
    68 John W. Taylor Avenue
    Alliston · Ontario · Canada · L9R 0E1 · gerry.lowry@abilitybusinesscomputerservices.com

    Websites:
    http://abilitybusinesscomputerservices.com
    http://gerrylowryprogrammer.com ~~ résumé & testimonials
    http://veganoccasions.com ~~ recipes by Susan
  • Re: File Upload

    03-28-2009, 4:04 PM
    • Contributor
      6,711 point Contributor
    • gerrylowry
    • Member since 07-03-2008, 1:46 AM
    • alliston ontario canada
    • Posts 2,281

    Follow up question:  did you just want to upload the sound file,
                                  or did you want it to play?
    g.

    Gerry Lowry, Principal
    Ability Business Computer Services ~~ Because it's your Business, our Experience Counts!
    68 John W. Taylor Avenue
    Alliston · Ontario · Canada · L9R 0E1 · gerry.lowry@abilitybusinesscomputerservices.com

    Websites:
    http://abilitybusinesscomputerservices.com
    http://gerrylowryprogrammer.com ~~ résumé & testimonials
    http://veganoccasions.com ~~ recipes by Susan
  • Re: File Upload

    03-28-2009, 4:09 PM
    • Contributor
      6,711 point Contributor
    • gerrylowry
    • Member since 07-03-2008, 1:46 AM
    • alliston ontario canada
    • Posts 2,281

    P.S.:  I find Google much better and at least an order of magnitude faster for searching forums.asp.net.

              Example:               ASP.Net mvc upload file how to
              http://www.google.com/search?q=aSP.Net+mvc+upload+file+how+to&rls=com.microsoft:en-ca:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7DELA_en

    Regards,
    Gerry (Lowry)

    Gerry Lowry, Principal
    Ability Business Computer Services ~~ Because it's your Business, our Experience Counts!
    68 John W. Taylor Avenue
    Alliston · Ontario · Canada · L9R 0E1 · gerry.lowry@abilitybusinesscomputerservices.com

    Websites:
    http://abilitybusinesscomputerservices.com
    http://gerrylowryprogrammer.com ~~ résumé & testimonials
    http://veganoccasions.com ~~ recipes by Susan
  • Re: File Upload

    03-28-2009, 8:00 PM
    • Member
      20 point Member
    • cerkit
    • Member since 10-17-2003, 8:47 PM
    • Atlanta, GA
    • Posts 10

    Oh, you mean like this...

    http://tinyurl.com/cbrfpk

     

    Michael Earls
    http://www.cerkit.com/
  • Re: File Upload

    03-28-2009, 8:04 PM
    • Member
      20 point Member
    • cerkit
    • Member since 10-17-2003, 8:47 PM
    • Atlanta, GA
    • Posts 10

    I'm going to do both. However, I'm just going to save the file out to the server and provide direct links to them...

    example:

    www.thewebsite.com/sounds/<Guid>.wav

    I saw a post earlier in the threads about file download, but I'm not sure if that applies to this case.

    Michael Earls
    http://www.cerkit.com/
  • Re: File Upload

    03-29-2009, 10:23 AM
    • Member
      78 point Member
    • niconel
    • Member since 03-29-2009, 9:10 AM
    • Beijing, China
    • Posts 36

     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.  Smile  (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");
    }
     
Page 1 of 1 (7 items)