Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • karen 186 posts 461 karma points
    Jun 05, 2015 @ 23:45
    karen
    0

    file upload in razor script - how to do it?

    I am trying to have form with a file upload in it.

    I have to have this form in a macro script. Everything has to be done through the Umbraco UI, so I don't see how I can write a server side controller. I only need to process the file, not save it to the server, if that makes any difference.

    I can't figure out how to make this work. I always get 0 count for files uploaded and no file name. Any other form values I put in get passed through, just not the file information.

    Here is my script:

       
    <form runat="server" id="resourceLoad" method="post">
    .csv file to import: <input type="file" name="inputFile"/><br />
    ID:<input type="text" name="id" /><br />
    <input id="resourceLoad" type="submit" value="GO" class="psButton" tabindex="205">
    </form>
    
    <br />
    <hr />
    @try {
        if ( IsPost ) {
            var fileName = "";
            if ( Request.Files.Count > 0 ) {
                fileName = Path.GetFileName( Request.Files[0].FileName );
            }
    
            <div>files uploaded = @Request.Files.Count </div>
            <div>file name = @fileName</div>
            <div>id = @Request["id"]</div>
        }
    } catch (Exception  ex) {
        <div>@ex.ToString()</div>
    }
    
    

    Example of output (after selecting a file with the file browser)

    files uploaded = 0
    file name =
    file name = test
  • karen 186 posts 461 karma points
    Jun 08, 2015 @ 16:40
    karen
    101

    ok - found the solution, had to add ' enctype="multipart/form-data"' to the form tag.

     <form action="" enctype="multipart/form-data" method="post">

     

    That is the difference between Friday afternoon brain dead and fresh Monday morning! :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies