Asp tutorial. net file upload methods
<% @ Page language = "C #" %>
<% @ Import Namespace = "System. IO" %>
<Script runat = "server">
Void UploadButton_Click (object sender, EventArgs e)
{
String savePath = @ "c :";
If (! Directory. Exists (savePath )){
String msg = "Response. Write (String. Format (msg, savePath ));
Response. End ();
}
If (FileUpload1.PostedFile! = Null)
{
String fileName = Path. GetFileName (FileUpload1.Value );
SavePath + = fileName;
FileUpload1.PostedFile. SaveAs (savePath );
UploadStatusLabel. InnerText = "File saved as: <I>" + savePath + "</I> ";
}
Else
{
UploadStatusLabel. InnerText = "You did not specify a file to upload .";
}
}
</Script>
<Html xmlns = "http://www.111cn.net">
<Head runat = "server">
<Title> HtmlInputFile </title>
</Head>
<Body>
<Div id = "pageContent">
<Form ID = "Form1" runat = "server">
<H3> Select a picture to upload:
<Hr/>
<B> Picture to upload </B> <br/>
<Input type = "file" id = "FileUpload1" runat = "server"/>
<Br/>
<Input runat = "server" id = "UploadButton" type = "submit" value = "Upload"
Onserverclick = "UploadButton_Click"/>
<Hr/>
<Span runat = "server" id = "UploadStatusLabel"/>
</Form>
</Div>
</Body>
</Html>
Method 2
<Script language = "C #" runat = "server">
Protected void PhotoSubmit (object o, EventArgs e ){
If (thePhoto. PostedFile! = Null ){
Try {
String filepath = "C: temp" + DateTime. Now. Ticks. ToString ();
ThePhoto. PostedFile. SaveAs (filepath );
Status. Text = "File saved as" + filepath;
}
Catch (Exception exc ){
Status. Text = "An Error occurred processing the file. Please try again." + exc. ToString ();
}
}
}
</Script>
<Form runat = "server" enctype = "multipart/form-data">
Please select an image to submit: <input id = "thePhoto" type = "file" runat = "server"> <br/>
<Input type = "button" runat = "server" value = "Proceed" OnServerClick = "PhotoSubmit"/>
<Asp: label runat = "server" id = "status"/>
</Form>
Method 3
<% @ Page Language = "C #" %>
<Script runat = "server">
Void UploadButton_Click (Object sender, EventArgs e ){
If (FileInput. PostedFile! = Null)
{
Try
{
FileInput. PostedFile. SaveAs ("c: test.txt ");
}
Catch (Exception exc)
{
Response. Write (exc. Message );
}
}
}
</Script>
<Html>
<Head>
</Head>
<Body>
<Form id = "Form1" method = "post" enctype = "multipart/form-data" runat = "server">
<Input id = "FileInput" type = "file" runat = "server"/>
<Asp: Button id = "UploadButton" onclick = "UploadButton_Click" runat = "server" Text = "Upload"> </asp: Button>
</Form>
</Body>
</Html>