Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Sending a File to a Web Server

Total Hit ( 2923)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Description

This is a working example of sending a file to a web server, and then processing the file from memory without saving it to a file.


ASP Page Code

You will need to create a new Web Form and add this ASP.NET code to it.

Click here to copy the following block
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
    Inherits="WebApplication2.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
   <HEAD>
       <title>WebForm1</title>
       <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
       <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
       <meta name="vs_defaultClientScript" content="JavaScript">
       <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
   </HEAD>
   <body MS_POSITIONING="GridLayout">
       <%
       if page.ispostback=false then
       %>
       <form runat="server" enctype="multipart/form-data" ID="Form1">
            <input type="file" id="file1" runat="server" NAME="file1"
                style="Z-INDEX: 103; LEFT: 324px; POSITION: absolute; TOP: 117px">
                
           <asp:Button id="btn1" runat="server" text="Upload" onclick="upload"
                style="Z-INDEX: 104; LEFT: 324px; POSITION: absolute; TOP: 172px" />
       </form>
       <%
       end if
       %>
   </body>
</HTML>

Codebehind VB.NET Code
Then in the WebForm1.aspx.vb file add this code.

Click here to copy the following block
Imports System.IO

Public Class WebForm1
  Inherits System.Web.UI.Page
  Protected WithEvents btn1 As System.Web.UI.WebControls.Button
  Protected WithEvents file1 As System.Web.UI.HtmlControls.HtmlInputFile

#Region " Web Form Designer Generated Code "

  'This call is required by the Web Form Designer.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

  End Sub

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init   
    InitializeComponent()
  End Sub

#End Region
  
  Public Sub Upload(ByVal sender As Object, ByVal e As System.EventArgs)

    'You can do this to save the file to the server if you like
    'file1.PostedFile.SaveAs("MyFile.csv")

    Dim r As New StreamReader(file1.PostedFile.InputStream())
    Dim strBuffer As String = r.ReadToEnd
    
    'DO SOMETHING HERE WITH strBuffer
    
    r.Close()
  End Sub
End Class

Conclusion

You are all ready to go. Run the project and select the file to upload from the file box. When the file gets sent to the server the Upload event fires and the contents of the file get written to the strBuffer variable. Then you can do that you like with it.

Also notice the commented code just below the Upload declaration. This code can be uncommented to save the file, to the disk instead if you wish.



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.