コード例 #1
0
        /// <summary>
        /// 下载文件.
        /// </summary>
        /// <param name="FileID">文件ID</param>
        public void Download(string FileID)
        {
            AttactFileVO file = g_AttactFile.QueryFilleByID(FileID);

            try
            {
                string   filePath = Server.MapPath(file.FILEPATH);
                FileInfo info     = new FileInfo(filePath);
                if (info.Exists)                //判斷文件是否存在,防止文件不存在導致WEB崩潰
                {
                    long fileSize = info.Length;
                    HttpContext.Response.Clear();

                    //指定Http Mime格式为压缩包
                    HttpContext.Response.ContentType = "application/x-zip-compressed";

                    // Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:
                    // Content-Disposition: attachment;filename=filename.txt
                    HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(info.Name));
                    //不指明Content-Length用Flush的话不会显示下载进度
                    HttpContext.Response.AddHeader("Content-Length", fileSize.ToString());
                    HttpContext.Response.TransmitFile(filePath, 0, fileSize);
                    HttpContext.Response.Flush();
                    HttpContext.Response.End();
                }
                else
                {
                    HttpContext.Response.Write("<script>alert('文件不存在!'); window.close();</script>");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
        /// <summary>
        /// 解析Excel返回DataTable
        /// </summary>
        public DataTable ExcelDataTable(HttpPostedFileBase file)
        {
            string           fileid       = SaveFile(file);                      //先保存文件,并获取文件ID
            BLAttactFile     g_AttactFile = new BLAttactFile();
            AttactFileVO     fileInfo     = g_AttactFile.QueryFilleByID(fileid); //根据文件ID获取文件内容
            string           filepath     = Server.MapPath(fileInfo.FILEPATH);
            string           strConn      = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0;";
            OleDbConnection  conn         = new OleDbConnection(strConn);
            OleDbDataAdapter oada         = new OleDbDataAdapter("select * from [Sheet1$]", strConn);
            DataSet          ds           = new DataSet();

            oada.Fill(ds, "ExcelInfo");
            oada.Dispose();
            conn.Close();
            DataTable dt = ds.Tables["ExcelInfo"].DefaultView.ToTable();

            return(dt);
        }
コード例 #3
0
        /// <summary>
        /// 保存文件.
        /// </summary>
        /// <param name="Files"></param>
        /// <returns></returns>
        public string SaveFile(HttpPostedFileBase file)
        {
            string       dir        = "UploadFile";
            AttactFileVO fileInfo   = new AttactFileVO();
            string       uploadPath = Server.MapPath("~/") + dir;

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);                         //判斷物理路徑是否存在,若不存在則創建此物理路徑.
            }
            string fileActualName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string extension      = Path.GetExtension(file.FileName);
            string filePath       = uploadPath + "\\" + fileActualName + extension;

            if (file.FileName != "")//上傳文件
            {
                file.SaveAs(filePath);
                string fileName = Path.GetFileName(file.FileName);
                fileInfo.FILENAME  = fileName;
                fileInfo.FILEPATH  = "/" + dir + "/" + fileActualName + extension;//william modified
                fileInfo.FILEID    = Guid.NewGuid().ToString();
                fileInfo.ENTRYUSER = LoginUser.USERID;
                if (g_AttactFile.InsertFile(fileInfo) == 1)
                {
                    return(fileInfo.FILEID);
                }
                else
                {
                    return("");
                }
            }
            else
            {
                return("");
            }
        }