• 0

Upload file in Asp.net


Question

I am trying to allow the user to upload a file to the server in Asp.net using vb. I have made progress using some scipts that I have found at various sites on the web. The script that I have works fine when uploadning txt files, but will not upload word or excel files regardless of their size. It doesn't even throw an error, it just says that the page can not be displayed. Is there some extra code that I need to add in order to other types of files?

PS - Word and Excel are not the only file types I want to be able to upload. They are just examples that I know don't work and need to. Here is my code:

<html>
<head>
<script language="VB" runat="server">

Sub Upload(Source As Object, e As EventArgs)

   If Not (myFile.PostedFile Is Nothing) Then

      Dim intFileNameLength as Integer
      Dim strFileNamePath as String
      Dim strFileNameOnly as String

      'Logic to find the FileName (excluding the path)
      strFileNamePath = MyFile.PostedFile.FileName
      intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
      strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)

      myFile.PostedFile.SaveAs("c:\inetpub\wwwroot\Project\documentation\" & strFileNameOnly)
      lblMsg.Text = "File Upload Success."
   lblFileContentType.Text = "Content type: " & MyFile.PostedFile.ContentType
   lblFileSize.Text = "File size: " & CStr(MyFile.PostedFile.ContentLength) & " bytes"

   End If
End Sub
</script>

</head>
<body>

<h3>File Upload</h3>

<form enctype="multipart/form-data" runat="server">
   File: <input id="myFile" type="file" runat="server">    <input type=button value="Upload" OnServerClick="Upload" runat="server">
   <asp:label id=lblMsg runat="server" />
   <asp:label id=lblFileContentType runat="server" />
   <asp:label id=lblFileSize runat="server" />
</form>

</body>
</html>

Link to comment
https://www.neowin.net/forum/topic/150015-upload-file-in-aspnet/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

see if this works

  <script language="VB" runat=server>

       Sub DoUpload(Sender As Object, e As System.EventArgs)
          Dim sPath as String
          Dim sFile as String
          Dim sFullPath as String
          Dim sSplit() as String
          Dim sPathFriendly as String
          
         
          
          'Upload to same path as script
          'Internet Anonymous User must have write permissions
          
          sPath = Server.MapPath(".")
         If Right(sPath, 1) <> "\" then 
 	 sPathFriendly = sPath 'Friendly path name for display
 	 sPath = sPath & "\"
  Else
 	 sPathFriendly = Left(sPath, Len(sPath) - 1)
  End If
 	 
          
          'Save as same file name being posted
          'The code below resolves the file name
          '(removes path info)
          sFile = txtUpload.PostedFile.FileName
          sSplit = Split(sFile, "\")
          sFile = sSplit(Ubound(sSplit))
          
          
          
          sFullPath = sPath & sFile
    Try
          txtUpload.PostedFile.SaveAs(sFullPath)
         lblResults.Text = "Upload of File " & sFile & " to " & sPathFriendly & " succeeded"
         
         Catch Ex as Exception
 	 
 	 lblResults.Text = "Upload of File " & sFile & " to " & sPathFriendly & " failed for the following reason: " & Ex.Message
         Finally
 	 lblResults.Font.Bold = True
 	 lblResults.Visible = true
         End Try
  
       End Sub

    </script>
<html>



    <body>

      <form enctype="multipart/form-data" runat=server>
 	 
          <FONT face = +1><B>

           Select File To Upload:</b></FONT>
           <input id="txtUpload" type=file runat=server>
 	 <P align = CENTER>
          <asp:button id=btnUpload Text="Upload File" OnClick="DoUpload" runat=server/>
 	 
          <hr noshade>

          <asp:label id="lblResults" Visible=false runat=server/>

     </form>

   </body>
</html>

  • 0

with the above code, I can upload any filetype (exe, zip, msi, doc, xls). with your code I get the same error.

Even the code from http://www.4guysfromrolla.com/webtech/091201-1.shtml works fine. Is there some type of restriction on your server?

Edited by primortal
  • 0

I am working off of the local server on my machine. As far as I know there are no restrictions. I tried to upload it to a couple different servers, and they both threw the error (Access to the path ...yadayada... is restricted. This makes since because on the servers FTP access is disabled for the we browser. At least this makes sense though, it gives me the error. On my local machine I am just getting the "Page can not be displayed" message. I can't figure out why it is doing this. The 4guysFromRolla script does the same thing. I tried that one first since 4guys is my mecca for asp.net advice. Do you have any more ideas? Maybe some permissions that need to be set to allow upload of these files?

  • 0

It does look and sound like the code should work. However, if you are able to save txt files, then it should be able to save others too. It's not a common feature that I am aware of where you can only save specific formats. You can go into IIS and make it spit out a more detailed message, so maybe then it will say you don't have permission or something more than page cannot be displayed

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Amazon Deals: Samsung Q990F Q900F Q800F 2025 Dolby Atmos soundbars with wireless subwoofers by Sayan Sen While separate AV receivers with hi-fi speakers are generally the preferred way to listed to music and watch movies/shows by audiophiles, the more general folks often prefer soundbars instead as they offer a capable all in one solution that is still plenty good. Currently Nakamichi is running discounts on multiple products from its Dragaon lineup as well as its Shockwafe model. If you are looking for more options to choose from Samsung has its Q-series products at lowest prices (purchase links towards the end of article). Q990F The flagship Q990F is an 11.1.4 system and the single subwoofer unit on it houses two opposite-facing 8-inch subwoofer drivers. Thus, together they move around the same amount of air as a single 12-inch subwoofer unit. In addition to increasing the bass by +3 dB, dual opposing drivers are also said to help reduce vibrations of the subwoofer cabinet by cancelling out the resonance. Samsung also says that the bass is "AI-optimized" but we are not sure if it actually helps or if it's just a buzz term here. Aside from the bass, dialogue in movies is the second most important thing, and Samsung claims great vocal clarity from its front speakers thanks to AVA (Active Voice Amplifier) Pro feature that is said to detect noise disturbances and amplify dialogue to make it more audible over such surrounding noises. Q900F Feature wise the Q900F is similar to the 990F model except it has fewer channels and it is a 7.1.2 setup. Finally the Q800F is a 5.1.2 system and it has a passive radiator instead of the additonal subwoofer driver unit. Get the Samsung Q series soundbars at the links below: Samsung Q990F 11.1.4ch Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Adaptive Sound (HW-Q990F, 2025): $1497.99 (Shipped and Sold by Amazon US) Samsung HW-Q900F 7.1.2 ch Wireless Dolby Atmos, Q-Symphony: $997.99 (Shipped and Sold by Amazon US) Samsung Q800F 5.1.2ch Q Series Soundbar + Subwoofer, Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Smart Integration (HW-Q800F, 2025): $697.99 (Shipped and Sold by Amazon US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • They are shifting into AI now. Don't you see?
  • Recent Achievements

    • One Month Later
      Helen Shafer earned a badge
      One Month Later
    • One Month Later
      ambani880 earned a badge
      One Month Later
    • Week One Done
      ambani880 earned a badge
      Week One Done
    • First Post
      artistro08 earned a badge
      First Post
    • First Post
      paul29 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      ATLien_0
      223
    3. 3
      Michael Scrip
      196
    4. 4
      Xenon
      160
    5. 5
      +FloatingFatMan
      138
  • Tell a friend

    Love Neowin? Tell a friend!