upload picture

Last post 11-28-2005 1:05 PM by ezguy. 4 replies.

Sort Posts:

  • upload picture

    11-28-2005, 4:04 AM
    • Member
      57 point Member
    • tatau
    • Member since 11-20-2005, 10:34 AM
    • Hawaii
    • Posts 26

    Edited by XIII

    Hello.  I'm new to ASP.Net and am learning asp.net 1.1 with vb.  I bought a book and made a working .aspx page that will upload to my own computer's hard drive.  Since I'm trying to learn this for websites that will be published on shared hosting I need help to alter the code so that it'll upload to a folder on my website rather than my own C:\ drive.

    Fpr example if my website name/folder is "WEBSITE" and the folder I want to upload the images is "imagesup"

    Here's the code I'm using:

    <%@ Import Namespace="System.IO" %>
    <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>

    <script language="VB" runat="server">
    Sub Upload(s As Object, e As EventArgs)
     If Not (txtUpload.PostedFile Is Nothing) Then
      Dim postedFile = txtUpload.PostedFile
      Dim filename As String = Path.GetFileName(postedFile.FileName)

      postedFile.SaveAs("c:\" & filename)
      lblMessage.Text = "File has been uploaded"
     End If
    End Sub
    </script>

    <form enctype="multipart/form-data" runat="server">
    <p><input id="txtUpload" type="file" runat="server" />
         </p>
       <p><input id="btnUpload" type="button" value="Upload" OnServerClick="Upload" runat="server" />
         </p>   <p><asp:Label id="lblMessage" runat="server" /></p>

  • Re: upload picture

    11-28-2005, 9:28 AM
    • Star
      12,508 point Star
    • Freakyuno
    • Member since 01-20-2005, 4:57 PM
    • Midwest - United States
    • Posts 1,952
    • TrustedFriends-MVPs

    Welcome to ASP.NET

    Take a look at the server.mappath() method.

    To upload a file to a shared hosting server, you need to know the absolute path where your website lives, this will accomplish that for you. :)

    It returns a string like c:\inetpub\wwwroot\account0010101\shared\....

    Thats just an example of course.

  • Re: upload picture

    11-28-2005, 9:57 AM
    • Contributor
      4,840 point Contributor
    • wunderkinder
    • Member since 10-31-2002, 5:42 PM
    • Black Hills of South Dakota
    • Posts 964
    postedFile.SaveAs(Server.MapPath("./") & filename)
    Jared Livingston
    MrLivingston.com - Take a look at the Livingston Family Web site.
  • Re: upload picture

    11-28-2005, 11:48 AM
    • Contributor
      4,100 point Contributor
    • Scott Mitchell
    • Member since 06-15-2002, 8:41 PM
    • San Diego, CA
    • Posts 707
    • ASPInsiders
      TrustedFriends-MVPs
    You can also use Request.PhysicalApplicationPath to get the physical path of the application root. (i.e., C:\Inetpub\wwwroot\MySite\, or whatever).

    A must-read article on paths is Rick Strahl's blog entry Making Sense of ASP.NET Paths.
    Happy Programming!


    -- Scott Mitchell
    -- mitchell@4guysfromrolla.com
    -- http://scottonwriting.net/sowblog/
    -- http://www.4GuysFromRolla.com/ScottMitchell.shtml
  • Stick out tongue [:P] Re: upload picture

    11-28-2005, 1:05 PM
    Answer
    • Member
      20 point Member
    • ezguy
    • Member since 11-28-2005, 12:06 PM
    • Posts 2
    postedFile.SaveAs(Server.MapPath("./") & filename)  is the way to go man.

    Make and meet new IT friends online at http://www.itfriendz.com
Page 1 of 1 (5 items)