Using system. IO;
1. File Upload
----------
Key points:
HTML section:
<Form ID = "form1" runat = "server" method = "Post" enctype = "multipart/form-Data">
<Input id = "fileupload" type = "file" runat = "server"/> <br/>
Backend CS button events
// String strfilefullname = system. Io. Path. getfilename (this. fileupload. postedfile. filename );
// This. fileupload. postedfile. saveas (server. mappath ("./xmlzip/") + strfilefullname );
2. File Download
----------
Set the download connection for the selectedindexchanged event of ListBox.
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 ();
}
}
Alternatively, you can change the text value of the label to achieve the super connection for file download after clicking.
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. Obtain the file list in the folder.
-----------
# Region get the list of currently available files
/// <Summary>
/// Obtain the list of currently available files
/// </Summary>
/// <Param name = "isalert"> whether a prompt is required </param>
Private void fn_getcurrfilelist (bool isalert)
{
Try
{
// Find the relevant ZIP file that belongs to unitcoding in the xmlzip folder.
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"); // check only. ZIP files
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 ("no files can be downloaded! ");
}
}
}
}
Catch (exception ex)
{
Ex. tostring ();
}
}
# Endregion
More details will be added later