The simple implementation of C # attachment upload

Source: Internet
Author: User
Tags servervariables

Submit.aspx

<%@ Page language= "C #" autoeventwireup= "true" aspcompat= "true" codefile= "Submit.aspx.cs" inherits= "Submit"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title> Untitled Page </title>
<body bgcolor= "#dae5e9" >
<form id= "Form1" runat= "Server" >
<table width= "780" border= "0" cellspacing= "0" cellpadding= "0" align= "center" >
<tr bgcolor= "#A3CEEE" >
&LT;TD height= ><strong> Select and upload accessories </strong></td>
</tr>
<tr>
&LT;TD height= ">1", click "Browse ..." button, select the attachment you want to submit;</td>
</tr>
<tr>
&LT;TD height= "><asp:fileupload" id= "FileUpload1" runat= "Server"/></td>
</tr>
<tr>
&LT;TD height= ">2", click <asp:button id= "btnupload" runat= "server" text= "upload" onclick= "btnupload_click"/> Button, Upload the attachment;</td>
</tr>
<tr>
&LT;TD height= ">3", if you need to submit more than one attachment, please repeat the steps 1 and 2 above;</td>
</tr>
<tr>
&LT;TD height= ">4", all the attachments after uploading, click <asp:button id= "btnsave" runat= "Server" text= "complete" onclick= "Btnsave_click"/ > button to return. </td>
</tr>
<tr bgcolor= "#A3CEEE" >
&LT;TD height= ><strong> Remove unwanted attachments </strong> (Please select the attachment you want to delete, then click the <strong> Remove </strong> button.) ) </td>
</tr>
<tr>
&LT;TD height= > Accessories list:
<asp:dropdownlist id= "Attachments" runat= "Server" >
<asp:listitem value= "-1" selected= "True" > Attachment list ...</asp:listitem>
</asp:DropDownList>
<asp:button id= "Btndelete" runat= "server" text= "delete" onclick= "Btndelete_click"/>
Total: <asp:label id= "Lbattachfilenumber" runat= "server" text= "0" width= "40px" ></asp:Label> Accessories
Total size: <asp:label id= "lbfilesize" runat= "server" text= "0" width= "40px" ></asp:Label> bytes
</td>
</tr>
</table>
</form>
</body>

**********************************************************************

Submit.aspx.cs

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.IO;

public partial class Submit:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{//involved in the database, need to display the attachment list and other related content (omitted here)
}
}

Number of attachments
int _attachfilenumber;
public int Attachfilenumber
{
get {return this._attachfilenumber;}
set {This._attachfilenumber = value;}
}

Total size of attachment
int _filesize;
public int FileSize
{
get {return this._filesize;}
set {this._filesize = value;}
}

directory where attachments are located
String _attachmentlinks = "";
public string Attachmentlinks
{
get {return this._attachmentlinks;}
set {this._attachmentlinks = value;}
}

protected void Btnupload_click (object sender, EventArgs e)
{
Try
{
if (this. FileUpload1.FileName.Length > 0)
{
string filename = "";

filename = this. Fileupload1.filename;
filename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + filename;

This. FileUpload1.PostedFile.SaveAs (request.servervariables["Appl_physical_path"] + "temp//" + filename);

THIS.ATTACHMENTS.ITEMS.ADD (filename) (new ListItem);

_attachfilenumber = Int. Parse (This.lbAttachFileNumber.Text) + 1;
This.lbAttachFileNumber.Text = _attachfilenumber.tostring ();

_filesize = Int. Parse (This.lbFileSize.Text) + this. FileUpload1.PostedFile.ContentLength;
This.lbFileSize.Text = _filesize. ToString ();

}
Else
{
Response.Write (' <script>alert (') please select an attachment. ') </script> ");
}
}
catch (Exception ex)
{
Response.Write (ex. Message.tostring ());
}
}
protected void Btnsave_click (object sender, EventArgs e)
{//involving the database, implementing the operations saved to the database (omitted here).

}
protected void Btndelete_click (object sender, EventArgs e)
{
Try
{
String ppath = request.servervariables["Appl_physical_path"] + "temp//" + this.attachments.SelectedItem;
This.attachments.Items.Remove (This.attachments.SelectedItem);
if (file.exists (Ppath))
{
FileInfo FileInfo = new FileInfo (Ppath);
_attachfilenumber = Int. Parse (This.lbAttachFileNumber.Text)-1;
int fsize = Convert.ToInt32 (FileInfo. Length);
_filesize = Int. Parse (This.lbFileSize.Text)-fsize;
This.lbAttachFileNumber.Text = _attachfilenumber.tostring ();
This.lbFileSize.Text = _filesize. ToString ();
File.delete (Ppath);
}
}
catch (Exception ex)
{
Response.Write (ex. Message.tostring ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.