The <INPUT TYPE=FILE> tag creates a file upload object with a text box and
Browse button. Users can enter a file path in the text box or click the
Browse button to browse the file system. The INPUT type=file element must
be enclosed within a FORM element.
A value must be specified for the NAME attribute of the INPUT type=file
element.
The METHOD attribute of the FORM element must be set to post.
The ENCTYPE attribute of the FORM element must be set to
multipart/form-data.
On the server side you would reference the file using the Request.Files
object. Here is a sample.
First I created a text file with the contents "Hello, World!" and saved
that to disk. Then I created a new project using ASP.NET. Here is my HTML
page:
<%@ Page language="c#" Codebehind="Def ault.aspx.cs" AutoEventWireup ="false"
Inherits="FileU pload._Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Defaul t</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5";>
</HEAD>
<body>
<form id="Default" method="post" runat="server"
enctype="multip art/form-data">
<INPUT type="file" runat="server" id="File1" name="File1">
<INPUT
id="Submit1" type="submit" value="Submit" name="Submit1" runat="server">
</form>
</body>
</HTML>
And here is my codebehind page:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
namespace FileUpload
{
/// <summary>
/// Summary description for _Default.
/// </summary>
///
public class _Default : System.Web.UI.P age
{
protected System.Web.UI.H tmlControls.Htm lInputFile File1;
protected System.Web.UI.H tmlControls.Htm lInputButton Submit1;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
string MyString = "";
int loop1;
int FileLen;
HttpFileCollect ion Files;
HttpPostedFile thisFile;
System.IO.Strea m myIS;
Files = Request.Files; // Load File collection into
HttpFileCollect ion
variable.
for(loop1=0; loop1<Files.Cou nt;loop1++)
{
thisFile = Request.Files[loop1];
Response.Write( "<b>"+thisFile. FileName+"</b><br>");
FileLen = thisFile.Conten tLength;
byte[] input = new byte[FileLen];
// Initialize the stream.
myIS = thisFile.InputS tream;
myIS.Read(input , 0, FileLen);
// Copy the byte array into a string.
for (int Loop1 = 0; Loop1 < FileLen; Loop1++)
MyString = MyString +(char)input[Loop1];
Response.Write( MyString+"<br>< br>");
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Submit1.Se rverClick += new
System.EventHan dler(this.Submi t1_ServerClick) ;
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void Submit1_ServerC lick(object sender, System.EventArg s e)
{
}
}
}
You browse to the file with the form, and upload it. On the server side you
can get at the file using the Request.Files object, you can access the file
name, and the contents are in the stream.
Hope this helps!
"Asha" <As**@discussio ns.microsoft.co m> wrote in message
news:1C******** *************** ***********@mic rosoft.com...
greetings,
i need to upload an .xls file using a secure connection. so i though of
using https... can i know how this can be done?