文件上传下载

上传
1           File.PostedFile.ContentLength //判断文件大小
2           File.PostedFile.FileName  //文件名称
3           File.PostedFile.ContentType  //文件类型
4           Server.MapPath()  //服务器路径对应物理路径
5           Directory.Exists(pathfile)  //验证文件夹是否存在
6           Directory.CreateDirectory(pathfile); //创建文件夹
7           url=pathfile+"1234567"+".ContentType";
8           file.PostedFile.SaveAs(url);   //保存到文件夹
View Code
下载
1  Response.ContentType="application/x-zip-compressed"; // 下载zip类型
2         Response.AddHeader("Content-Disposition","attachment;filename=z.zip"); //以xx方式下载 attachment 附件
3         string filePath =  Server.MapPath("服务器路径")  //服务器路径对应物理路径
4         Response.TransmitFile(fileName); //下载
View Code
循环下载
 1  
 2         string filePath = Server.MapPath("服务器路径");
 3         System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
 4 
 5         if (fileInfo.Exists == true)
 6         {
 7                 const long readSize = 102400;
 8                 byte[] buffer = new byte[readSize];
 9                 Response.Clear();
10                 System.IO.FileStream stram= System.IO.File.OpenRead(filePath);
11                 long allszie = stram.Length;//获取下载的文件总大小
12                 Response.ContentType = "application/octet-stream";
13                 Response.AddHeader("Content-Disposition", "attachment; filename=" + "filename.xx");
14                 while (allszie > 0 && Response.IsClientConnected)
15                 {
16                     int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(readSize));//读取的大小
17                     Response.OutputStream.Write(buffer, 0, lengthRead);
18                     Response.Flush();
19                     allszie = allszie - lengthRead;
20                 }
21               Response.Close();
22         }
View Code
Excel转DataSet   
string str = "Provider=Microsoft.Ace.OLEDB.12.0;" + "data source=" + savePath + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";    
 1 OleDbConnection conn= new OleDbConnection(strcon);
 2      try
 3      {
 4          string strsql = string.Format("SELECT * FROM  Sheet1$");
 5          conn.Open();
 6          OleDbDataAdapter command= new OleDbDataAdapter(strsql, conn);
 7          DataSet ds = new DataSet();
 8          command.Fill(ds, "[" + tableName + "$]");
 9          excelConn.Close();       
10          return ds;
View Code

 

posted @ 2017-02-22 09:54  SSS.M.AAA  阅读(186)  评论(0编辑  收藏  举报