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

Source: Internet
Author: User
Tags file upload rar

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");
  }
}

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.