zoukankan      html  css  js  c++  java
  • Asp.net上传文件后台通过二进制流发送到其他Url保存

    实际情况一般有单独的站点存放静态文件,比如图片、office文档等。A站点的操作需要上传文件到B站点,

    下面介绍一种方法通过System.Net.WebClient类的UploadData方法 。

    uploadFile.aspx html:

    <form id="form1" runat="server">
        <div>
            <asp:FileUpload runat="server" ID="fileUp" AllowMultiple="true" /> 
            <asp:Button runat="server" ID="btnLoad" Text="Upload" OnClick="btnLoad_Click" />
        </div>
        </form>

    uploadFile.cs 后台代码

     protected void btnLoad_Click(object sender, EventArgs e)
            {
                if (fileUp.HasFile)
                {
                    var list = fileUp.PostedFiles;
                    foreach (var item in list)
                    {
                        string fileDoc = System.IO.Path.GetExtension(item.FileName).ToLower(); //后缀名
                        int fileLength = (int)item.InputStream.Length;//InputStream.Length;
    
                        byte[] byteFile = new byte[fileLength];
                        item.InputStream.Read(byteFile, 0, fileLength);
                        item.InputStream.Seek(0, System.IO.SeekOrigin.Begin); //读取流之后设置设置当前流的位置,因为md5加密也需要用到该流
    
                        string md5Value =  Com.MD5.MyMD5.getMd5Hash(item.InputStream); //得到上传文件流的MD5string url = string.Format("http://localhost:4884/SaveFile.ashx?name=" + md5Value + "&ext=" + fileDoc);  //保存流的处理文件路径
    
                        System.Net.WebClient wc = new System.Net.WebClient();
                        byte[] bts = wc.UploadData(url, "POST", byteFile);
                        wc.Dispose();
                        string filePath = System.Text.Encoding.UTF8.GetString(bts); //获取SaveFile.ashx返回
                    }
                }
            }
    Com.MD5.MyMD5.getMd5Hash方法请看:我的C# MD5摘要算法、哈希算法

    SaveFile.ashx 处理程序

     /// <summary>
        /// SaveFile 的摘要说明
        /// </summary>
        public class SaveFile : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                lock (context.Request.InputStream)
                {
                    HttpRequest Request = context.Request;
                    string path = ""; string filenName = "";
                    string re = "";
                    if (Request.HttpMethod.ToLower() == "post")
                    {
                        try
                        {
                            using (Request.InputStream)
                            {
                                string name = Request["name"];
                                string s = Request["ext"];
                                byte[] bytes = new byte[Request.InputStream.Length];
                                Request.InputStream.Read(bytes, 0, bytes.Length); 
                                Request.InputStream.Seek(0, SeekOrigin.Begin);//设置当前流的位置
                                //Request.InputStream.Position = 0;
                                Request.InputStream.Flush();
                                Request.InputStream.Close();
                                Request.InputStream.Dispose();
                                Random rnd = new Random();
                                int n = rnd.Next(1000, 9999);
                                //filenName = "/file/" + n.ToString() + "-" + DateTime.Now.ToString("yyyyMMddHHmmssss") + s;
                                filenName = "/file/"+name + s;
                                path = context.Server.MapPath(filenName);
    
                                FileStream fs = new FileStream(path, FileMode.Create);
                                BinaryWriter bw = new BinaryWriter(fs);
                                bw.Write(bytes);
                                bw.Close();
                                fs.Close();
                                re = filenName;
    
                               //System.Threading.Thread.Sleep(500);
                            }
                        }
                        catch (Exception ex)
                        {
                            
                        }
                    }
    
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(re);
                    context.Response.End();
                }
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }

    HttpFileCollection:

       void save(HttpRequest Request)
            {
                var file = Request.Files[0];
                var filenName = "/file/" + file.FileName;
                var path = HttpContext.Current.Server.MapPath(filenName);
                file.SaveAs(path);
            }
    
    
    
    
    
    
  • 相关阅读:
    深入浅出AQS之组件概览
    深入浅出AQS之条件队列
    深入浅出AQS之共享锁模式
    深入浅出AQS之独占锁模式
    Android中RecyclerView用法,一步一步教你如何使用RecyclerView以及带你走过编码中可能会出现的坑~
    检测jquery是否正确引入
    对于使用JDBC连接mysql数据时The server time zone value '¤¤°ê¼Ð·Ç®É¶¡'...的异常问题解决。
    MYSQL8.0以上版本ROOT密码报错及修改
    MYSQL安装教程
    linux centos安装nginx1.7.4
  • 原文地址:https://www.cnblogs.com/tongyi/p/4274354.html
Copyright © 2011-2022 走看看