zoukankan      html  css  js  c++  java
  • C#实现HTTP请求文件下载,GET、POST请求的数据流接收

    做项目的时候由于插件Phaser请求audio的时候,不允许跨域,具体提示====》
    已拦截跨源请求:同源策略禁止读取位于 http://ttyouni.com/1.mp3 的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。
    幸亏只是音乐,要是图片也不允许跨域,就麻烦了。因为以前一直使用图片上传,所以代码也是参照着那里写的,结果,拿到的文件一直是损坏的。
    其中看到stream的Length的显示是出现异常,虽然知道是因为网络数据流读取的问题,但是怎么写还是不清楚。
    C++的buffer写法倒是会,但是C#的一直没写过。网上搜,关键词一直不对,搜了老久的c#网络请求数据流接收,没有一个有用。
    哦,后来搜到个streamreader,可惜人家写的接收类型是string...还是╮(╯﹏╰)╭不会。最后,还是老大出马,拿了个网上的参考地址。
    最后才写好的,总觉得一把辛酸泪。

    参考的地址:http://www.jb51.net/article/57068.htm

     1 public string CopyFileByUrl(string url)
     2         {
     3             string name = url.Substring(url.LastIndexOf('/') + 1);//获取名字
     4             string fileFolder = UploadConfigContext.UploadPath;
     5             string filePath = Path.Combine(fileFolder, name);//存放地址就是本地的upload下的同名的文件
     6             if (!Directory.Exists(fileFolder))
     7                 Directory.CreateDirectory(fileFolder);
     8 
     9             string returnPath = GetSimplePath(filePath);//需要返回的路径
    10             if (File.Exists(filePath))
    11             {//如果已经存在,那么就不需要拷贝了,如果没有,那么就进行拷贝
    12                 return returnPath;
    13             }
    14             HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
    15             request.Method = "GET";
    16             request.ProtocolVersion = new Version(1, 1);
    17             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    18             if (response.StatusCode == HttpStatusCode.NotFound)
    19             {
    20                 return string.Empty;//找不到则直接返回null
    21             }
    22             // 转换为byte类型
    23             System.IO.Stream stream = response.GetResponseStream();
    24 
    25 
    26             //创建本地文件写入流
    27             Stream fs = new FileStream(filePath, FileMode.Create);
    28             byte[] bArr = new byte[1024];
    29             int size = stream.Read(bArr, 0, (int)bArr.Length);
    30             while (size > 0)
    31             {
    32                 fs.Write(bArr, 0, size);
    33                 size = stream.Read(bArr, 0, (int)bArr.Length);
    34             }
    35             fs.Close();
    36             stream.Close();
    37             return returnPath;
    38         }
    Copy代码
            public string GetSimplePath(string path)
            {
                //E:Uploadcmsday_1508131.jpg
                path = path.Replace(@"", "/");
                int pos = path.IndexOf("Upload");
                if (pos != -1)
                {
                    pos = pos - 1;//拿到前面那个/,这样为绝对路径,直接保存在整个项目下的upload文件夹下
                    return path.Substring(pos, path.Length - pos);
                }
                return "";
            }
    
  • 相关阅读:
    跨浏览器右键复制实现
    在eclipse里如何快速定位到某一行?
    为数据库中的表添加字段步骤
    一键安装openstack juno 之controller node.
    Linux的/etc/services文件的作用?
    docker and ssh issues
    yum change source repo centos共存安装sun jdk6和jdk7
    random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串
    OpenStack,ceph
    那些证书相关的玩意儿(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等)[zz]
  • 原文地址:https://www.cnblogs.com/danlis/p/6006577.html
Copyright © 2011-2022 走看看