uploading using https

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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="Default.aspx.cs" AutoEventWireup="false"
Inherits="FileUpload._Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Default</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5";>
</HEAD>
<body>
<form id="Default" method="post" runat="server"
enctype="multipart/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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace FileUpload
{
/// <summary>
/// Summary description for _Default.
/// </summary>
///
public class _Default : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

string MyString = "";
int loop1;
int FileLen;
HttpFileCollection Files;
HttpPostedFile thisFile;
System.IO.Stream myIS;
Files = Request.Files; // Load File collection into
HttpFileCollection
variable.
for(loop1=0; loop1<Files.Count;loop1++)
{
thisFile = Request.Files[loop1];
Response.Write("<b>"+thisFile.FileName+"</b><br>");

FileLen = thisFile.ContentLength;
byte[] input = new byte[FileLen];

// Initialize the stream.
myIS = thisFile.InputStream;
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(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Submit1.ServerClick += new
System.EventHandler(this.Submit1_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Submit1_ServerClick(object sender, System.EventArgs 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!
 
Back
Top