扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
IServiceProviderprovider=(IServiceProvider)HttpContext.Current; HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); byte[]bs=wr.GetPreloadedEntityBody(); . if(!wr.IsEntireEntityBodyIsPreloaded()) { intn=1024; byte[]bs2=newbyte[n]; while(wr.ReadEntityBody(bs2,n)>0) { .. } } |
public class FileUpLoad { public FileUpLoad() {} /**//// <summary> /// 上传文件名称 /// </summary> public string FileName { get { return fileName; } set { fileName = value; } } private string fileName; /**//// <summary> /// 上传文件路径 /// </summary> public string FilePath { get { return filepath; } set { filepath = value; } } private string filepath; /**//// <summary> /// 文件扩展名 /// </summary> public string FileExtension { get { return fileExtension; } set { fileExtension = value; } } private string fileExtension; } |
<?xml version="1.0" encoding="gb2312" ?> <Application> <FileUpLoad> <Format>.jpg|.gif|.png|.bmp</Format> </FileUpLoad> </Application> 这样我们就可以开始写我们的上传文件的方法了,如下: public FileUpLoad UpLoadFile(HtmlInputFile InputFile,string filePath,string myfileName,bool isRandom) { FileUpLoad fp = new FileUpLoad(); string fileName,fileExtension; string saveName; // //建立上传对象 // HttpPostedFile postedFile = InputFile.PostedFile; fileName = System.IO.Path.GetFileName(postedFile.FileName); fileExtension = System.IO.Path.GetExtension(fileName); // //根据类型确定文件格式 // AppConfig app = new AppConfig(); string format = app.GetPath("FileUpLoad/Format"); // //如果格式都不符合则返回 // if(format.IndexOf(fileExtension)==-1) { throw new ApplicationException("上传数据格式不合法"); } // //根据日期和随机数生成随机的文件名 // if(myfileName != string.Empty) { fileName = myfileName; } if(isRandom) { Random objRand = new Random(); System.DateTime date = DateTime.Now; //生成随机文件名 saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + Convert.ToString(objRand.Next(99)*97 + 100); fileName = saveName + fileExtension; } string phyPath = HttpContext.Current.Request.MapPath(filePath); //判断路径是否存在,若不存在则创建路径 DirectoryInfo upDir = new DirectoryInfo(phyPath); if(!upDir.Exists) { upDir.Create(); } // //保存文件 // try { postedFile.SaveAs(phyPath + fileName); fp.FilePath = filePath + fileName; fp.FileExtension = fileExtension; fp.FileName = fileName; } catch { throw new ApplicationException("上传失败!"); } //返回上传文件的信息 return fp; } |
public byte[] UpLoadFile(HtmlInputFile f_IFile) { //获取由客户端指定的上传文件的访问 HttpPostedFile upFile=f_IFile.PostedFile; //得到上传文件的长度 int upFileLength=upFile.ContentLength; //得到上传文件的客户端MIME类型 string contentType = upFile.ContentType; byte[] FileArray=new Byte[upFileLength]; Stream fileStream=upFile.InputStream; fileStream.Read(FileArray,0,upFileLength); return FileArray; } |
<add verb="*" path="openfile.aspx" type="RuixinOA.Web.BaseClass.OpenFile, RuixinOA.Web"/> |
using System; using System.Data; using System.Web; using System.IO; using Ruixin.WorkFlowDB; using RXSuite.Base; using RXSuite.Component; using RuixinOA.BusinessFacade; namespace RuixinOA.Web.BaseClass { /**//// <summary> /// NetUFile 的摘要说明。 /// </summary> public class OpenFile : IHttpHandler { public void ProcessRequest(HttpContext context) { //从数据库中取出要下载的文件信息 RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager(); EntityData data = os.GetFileDetail(id); if(data != null && data.Tables["RX_OA_File"].Rows.Count > 0) { DataRow dr = (DataRow)data.Tables["RX_OA_File"].Rows[0]; context.Response.Buffer = true; context.Response.Clear(); context.Response.ContentType = dr["CContentType"].ToString(); context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString())); context.Response.BinaryWrite((Byte[])dr["CContent"]); context.Response.Flush(); context.Response.End(); } } public bool IsReusable { get { return true;} } } } |
HttpWebRequest hwq = (HttpWebRequest)WebRequest.Create("http://localhost/pwtest/webform1.aspx"); HttpWebResponse hwr = (HttpWebResponse)hwq.GetResponse(); byte[] bytes = new byte[hwr.ContentLength]; Stream stream = hwr.GetResponseStream(); stream.Read(bytes,0,Convert.ToInt32(hwr.ContentLength)); //HttpContext.Current.Response.BinaryWrite(bytes); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者