文件的上传下载
2014-12-03 16:59 taozsay 阅读(116) 评论(0) 收藏 举报将文件转换成二进制保存到数据库。
protected void btnUpload_Click(object sender, EventArgs e) { string FileUploadPath = "../../Upload"; HiddenSysNew.Value = ""; List<SystemChangeFile> list = new List<SystemChangeFile>(); System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; System.Text.StringBuilder strmsg = new System.Text.StringBuilder(""); string[] rd = Request.Form[1].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述 int ifile; int num = 0; for (ifile = 0; ifile < files.Count; ifile++) { if (files[ifile].FileName.Length > 0) { System.Web.HttpPostedFile postedfile = files[ifile]; if (postedfile.ContentLength / 1024 > 1024 * 40)//单个文件不能大于1024*10k { strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于40M<br>"); break; } //string fex = Path.GetExtension(postedfile.FileName).ToUpper(); //if (fex != ".DOC" && fex != ".DOCX" && fex != ".XLSX" && fex != ".XLS" && fex != ".JPG") //{ // strmsg.Append(Path.GetFileName(postedfile.FileName) + "---文件格式不正确"); // break; //} num = num + 1; } } //文件格式不对 if (!string.IsNullOrEmpty(strmsg.ToString())) { ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + strmsg + "!');</script>"); return; } if (num <= 0) { ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertAndNewLoad('没有上传文件!');</script>"); return; } if (strmsg.Length <= 0)//说明图片大小和格式都没问题 { //以下为创建图库目录 string dirpath = Server.MapPath(FileUploadPath); if (Directory.Exists(dirpath) == false) { Directory.CreateDirectory(dirpath); } Random ro = new Random(); int name = 1; for (int i = 0; i < files.Count; i++) { System.Web.HttpPostedFile myFile = files[i]; string FileName = ""; string FileExtention = ""; FileName = System.IO.Path.GetFileName(myFile.FileName); string stro = ro.Next(100, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片 string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro; string ppath = ""; if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库 { Stream fileStream = files[i].InputStream; //用于读取上传的数据 FileExtention = System.IO.Path.GetExtension(myFile.FileName); ppath = dirpath + @"\" + NewName + FileExtention; //myFile.SaveAs(ppath); Guid FileId = Guid.NewGuid(); int strFileLen = FileName.Length;//获取文件的大小 SystemChangeFile def = new SystemChangeFile(); def.FileId = FileId; def.FileName = FileName; def.FileDesc = rd[i]; //def.FilePath = FileUploadPath + "/" + NewName + FileExtention; if (!string.IsNullOrEmpty(HfType.Value.Trim())) { def.ErpType = int.Parse(HfType.Value.Trim()); } byte[] filebuffer = new byte[strFileLen]; //准备一个和文件大小一样的缓存 BinaryReader br = new BinaryReader((Stream)fileStream); filebuffer = br.ReadBytes((int)fileStream.Length); def.FileType = files[i].ContentType; def.FileContent = filebuffer; list.Add(def); HiddenSysNew.Value += ReturnTr(def); } name = name + 1;//用来重命名规则的变量 } new Biz.Biz_SystemChanges().AddFile(list); ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertAndNewLoad('上传成功!');</script>"); } }
文件的下载
#region 下载附件 /// <summary> /// 下载附件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnDownFile_Click(object sender, EventArgs e) { Guid d; if (Guid.TryParse(HFileValue4.Value.Trim(), out d)) { SystemChangeFile def = Biz_SystemChanges.Biz_SystemChanges.Biz_QuerySystemRightFile(new SystemChangeFile() { FileId = d }).First(); Byte[] fileBytes = def.FileContent; Response.Buffer = true; Response.ClearContent(); Response.ClearHeaders(); //指定http名称和值 Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(def.FileName, System.Text.Encoding.UTF8)); //指定文件类型 Response.ContentType = "application/octet-stream"; //Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.BinaryWrite(fileBytes); Response.Flush(); Response.End(); } }
public void DownFile(string path) { string strFile = string.Format(@"{0}", path); using (FileStream fs = new FileStream(strFile, FileMode.Open)) { byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("data.xml", System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } }
#endregion
作者:taoz
出处:www.cnblogs.com/bigbrid
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
本文如对您有帮助,还请多帮 【推荐】 下此文。
如果喜欢我的文章,请关注我的公众号
浙公网安备 33010602011771号