C # File operations (upload download delete file list ...)

Source: Internet
Author: User
Tags file upload rar tostring trim zip
Upload | download

Using System.IO;

1. File Upload
----------
The following points:
HTML section:
<form id= "Form1" runat= "Server" method= "POST" enctype= "Multipart/form-data" >
<input id= "FileUpload" type= "file" runat= "server"/><br/>
Background CS Partial button Event
String strfilefullname = System.IO.Path.GetFileName (this. FileUpLoad.PostedFile.FileName);
This. FileUpLoad.PostedFile.SaveAs (Server.MapPath ("./xmlzip/") + Strfilefullname);

2. File download
----------
ListBox SelectedIndexChanged Event settings Related download connection
protected void Lst_downloadfilelist_selectedindexchanged (object sender, EventArgs e)
{
Try
{
String strjs = "window.open (' xmlzip/";
Strjs + + This.lst_DownLoadFileList.SelectedItem.Text.Trim ();
Strjs + = "'); return false; ";
THIS.IMGBTN_DOWNLOADFILE.ATTRIBUTES.ADD ("onclick", Strjs);
}
catch (Exception ex)
{
Ex. ToString ();
}
}
Or you can change the label of the text value to achieve a click to implement the file download super connection
This. Label1.Text = "<a href=\" xmlzip/a.rar\ ">a.rar</a>"

3. File deletion
---------
String strFilePath = Server.MapPath (".. /countryflowmgr/xmlzip/"+this.lst_downloadfilelist.selecteditem.text.trim ());
if (file.exists (strFilePath))
{
File.delete (strFilePath);
if (file.exists (strFilePath))
{
Response.Write ("OK");
}
Else
{
Response.Write ("OK");
}
}

4. Get the file list under the folder
-----------
#region get a list of currently available files
<summary>
Get a list of currently available files
</summary>
<param name= "Isalert" > whether pop-up prompts are required </param>
private void Fn_getcurrfilelist (bool isalert)
{
Try
{
Find a related zip file in the Xmlzip folder that is subordinate to its own unitcoding
String strxmlzipdirectory = Server.MapPath (".. /xmlzip/");
if (directory.exists (strxmlzipdirectory))
{
DirectoryInfo di = new DirectoryInfo (environment.currentdirectory);
DirectoryInfo di = new DirectoryInfo (strxmlzipdirectory);

fileinfo[] FI = di. GetFiles ("*.zip")//Only check the. zip file
if (FI. Length > 0)
{
Lst_DownLoadFileList.Items.Clear ();
foreach (FileInfo tmpfi in FI)
{
ListItem Tmpitem = new ListItem ();
Tmpitem.text = Tmpfi.name;
LST_DOWNLOADFILELIST.ITEMS.ADD (Tmpitem);
}
Lst_downloadfilelist.selectedindex = 0;
}
Else
{
if (Isalert)
{
Response.Write ("Check no downloadable files!");
}
}
}
}
catch (Exception ex)
{
Ex. ToString ();
}
}
#endregion




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.