FileUpload Path

Discussion in 'ASP.NET / ASP.NET Core' started by infokarenet, Jan 26, 2008.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hi experts,


    I have an account in discountasp.net. I made folder "UserFiles". I have wriitten FileUpload codes w/ ASP.NET as follows:


    Then I have this error message: ERROR: Could not find a part of the path 'c:\UserFiles\mmc.gif'.


    How do I solve this?


    Thanks for your kind advices.


    =============== my codes =====================


    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click


    If FileUpload1.HasFile Then


    Try


    FileUpload1.SaveAs("\UserFiles\" & FileUpload1.FileName)


    lblUpload.Text = "File name: " & _


    FileUpload1.PostedFile.FileName & "" & _


    "File Size: " & _


    FileUpload1.PostedFile.ContentLength & " kb" & _


    "Content type: " & _


    FileUpload1.PostedFile.ContentType


    Catch ex As Exception


    lblUpload.Text = "ERROR: " & ex.Message.ToString()


    End Try


    Else


    lblUpload.Text = "You have not specified a file."


    End If


    End Sub
     
  2. Youshould use MapPath and/or the root directive, i.e. ~\UserFiles\
     
  3. Hi wisemx,

    Then I havethis error message: ERROR: The SaveAs method is configured to require a rooted path, and the path '~\UserFiles\index.dat' is not rooted.

    Your kind advice would be appreciated.


    ==================== Modified codes:==============




    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click


    If FileUpload1.HasFile Then


    Try


    FileUpload1.SaveAs("~\UserFiles\" & FileUpload1.FileName)


    lblUpload.Text = "File name: " & _


    FileUpload1.PostedFile.FileName & "" & _


    "File Size: " & _


    FileUpload1.PostedFile.ContentLength & " kb" & _


    "Content type: " & _


    FileUpload1.PostedFile.ContentType


    Catch ex As Exception


    lblUpload.Text = "ERROR: " & ex.Message.ToString()


    End Try


    Else


    lblUpload.Text = "You have not specified a file."


    End If


    End Sub
     
  4. I noticed that after posting and wondered if you are bound to ASP.NET 1.1 for this.
    If you can use ASP.NET 2.0 methods I can post some code for you.
    Salute,
    Mark
     
  5. Hi Mark,

    I use asp.net 2.0 for this.
    If you post you codes, taht would be very much appreciated.
    FYI, I need this for my hosting site at discountasp.net. That means if I use my codes at local pc, it's uplaoded to my local disk.

    Thanks,
     
  6. Hi Mark,

    I cannot find right answer from this link.
    I think my codes are fine except hosting site's Save directory part. If I use these codes at local PC, it is working fine. But I do not know it for hosting site.
    Would you please advise me it?

    Thanks,





    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click


    If FileUpload1.HasFile Then


    Try


    FileUpload1.SaveAs("\UserFiles\" & FileUpload1.FileName)


    lblUpload.Text = "File name: " & _


    FileUpload1.PostedFile.FileName & "" & _


    "File Size: " & _


    FileUpload1.PostedFile.ContentLength & " kb" & _


    "Content type: " & _


    FileUpload1.PostedFile.ContentType


    Catch ex As Exception


    lblUpload.Text = "ERROR: " & ex.Message.ToString()


    End Try


    Else


    lblUpload.Text = "You have not specified a file."


    End If


    End Sub
     
  7. You should have been able to find a ton of examples with that link but I'll give you a bit of code.

    Something you want to do first is Dim the string and build the path.

    Dim filepath As String = "~/MyFiles/" & thefile.FileName
    thefile.SaveAs(MapPath(filepath))

    <asp:FileUpload id="thefile" Runat="server" />

    See how that would work?

    If you can't get that working I'll create an ASP.NET v2 code file for you.
     
  8. Hi Mark,


    If you generate the codes for me, that would be great.


    Thanks for your kind help.
     
  9. OK, a few details about the files that will be uploaded will help.

    Very large files? (Y/N)

    Images, Binaries? Do you want to restrict to certain file extensions?
    Salute,
    Mark
     
  10. Hi Mark,


    Size : very large file.


    File Extension : any type (no restriction)





    Thanks,
     
  11. Simplest form of FileUpload implementation would be as follows:

    <%@ Page Language='C#' %>

    <script runat='server'>
    protected void UploadButton_Click(object sender, EventArgs e)
    {
    string filename=FileUpload1.FileName;
    //Server.MapPath('/') provides path to your root directory,where in I have an Upload Directory
    FileUpload1.PostedFile.SaveAs(Server.MapPath('/')+'/Upload/' + filename);

    }
    </script>
    <html>

    <form runat='server'>
    <asp:FileUpload ID='FileUpload1' runat='server' />
    <asp:Button ID='UploadButton' runat='server' Text='Upload' OnClick='UploadButton_Click' />
    </form>
    </body>
    </html>

    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     
  12. Thanks a lot Mark and Vikram.


    I received Upload Path information from discountasp.net, so it's working fine.


    Again thanks a lot Mark and Vikram


    === path information from discountasp.net ========
    Dear Customer,

    Please use the complete path. To obtain the path to your root, log into the control panel at my.discountasp.net and click Account Info/Edit on the left hand side. Your root path will be visible there.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page