Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

FileTransfer Library in VB.NET

0.00/5 (No votes)
6 Jun 2004 1  
This class is designed to upload and download files via browser through asp.net and vb.net

License

Free to use and modify as long as header stays in place.

Terms and Conditions

For Use, Copy, Distribution and Modification: THIS CODE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

How to use

Compile code into a .net dll assembly and put into your website home (bin) folder. Also transfer Interop.Scripting.dll and adodb.dll into your bin folder also.

Permissions

Make sure your permissions are correctly set for whatever folder you are writing or reading from.

Code For Download

<%@ Import Namespace = "FileTransfer.FileTransfer"%> 
<script language="VB"runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
  If Not (Page.IsPostBack)
    Dim rs as New File
    Dim Downloads as string = rs.Download("test.txt", "c:/", 100000)
    'Dim Downloads as string = rs.Download(Request.querystring

    '("Link"), "c:/", 100000)

  End If
End Sub
</script>

Code For Upload

<%@ Import Namespace = "FileTransfer.FileTransfer"%> 
<script language="VB"runat="server">
Sub Upload_File (Sender as Object, e as EventArgs)
  Dim FileControl as System.Web.UI.HtmlControls.HtmlInputFile = MyFile
  Dim rs as New File()
  Dim ReturnMessage as String = rs.Upload("c:\", FileControl) 

  Context.response.write(ReturnMessage)
End Sub
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Upload File</title>
<meta http-equiv="Content-Type"content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="upload.aspx"method="post"enctype="Multipart/Form-Data"runat="server">
<input id="MyFile"type="File"runat="Server"size="25">
<br><br><asp:button ID="SubmitButton"text="Upload"runat="server"
   OnClick="Upload_File"style="cursor:hand; 
   background-color: #0000A0; color: #ffffff;"/>
</form>
</body>
</html>

Details

Well lets see here, do you think you have had enough of file transfer via browser. I don't think so! But wait hold up don't leave just yet. My FileTransfer class has a different approach to the whole thing. Lets get started. My download function uses both adodb and Interop.Scripting to allow files to be downloaded in chunks. Yes! chunks of byte data that you can specify the size of. This allows you to surpass the 4mb cache limit for downloads via browser. I also designed the download function to force the browser to download the file by setting the ContentType ="application/octet-stream". I use adodb to make a binary stream of the file. From there I loop through pulling the allotted chunk sizes and flush the stream on each pass making sure the data never overwhelms the cache size. When the data has finished sending, I check to see if there is left over data to be sent, that maybe wasn't a full block size. I send that data also to complete the download.

My upload function is not as special. It uses the common way Microsoft explains on how to do it. I could have easily made it using the same philosophy as the download function, to give me more control. But both ways are possibly. I am tired now so I am going to finish with that. If anyone needs any help let me know. I am going to sleep now. LOL

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here