下载网络文件,网络流转换为内存流

string fileext = CSA_BLL.Common.Utils.GetFileExt(filePath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filePath);
request.Method = "GET";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{
Stream rs = response.GetResponseStream();
//网络流转换为内存流
var ms = StreamToMemoryStream(rs);
ms.Seek(0, SeekOrigin.Begin); int buffsize = (int)ms.Length; //rs.Length 此流不支持查找,先转为MemoryStream
byte[] bytes = new byte[buffsize];

ms.Read(bytes, 0, buffsize);
ms.Flush(); ms.Close();
rs.Flush(); rs.Close();
//以文件流的方式下载
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/octet-stream";
if (HttpContext.Request.UserAgent.ToUpper().Contains("MSIE") ||
HttpContext.Request.UserAgent.ToUpper().Contains("TRIDENT") ||
HttpContext.Request.UserAgent.ToUpper().Contains("EDGE"))
{
fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
}
else
{
fileName = fileName.Replace(" ", "_");
}
//fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName + "." + fileext));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

}
}
return new EmptyResult();
}

MemoryStream StreamToMemoryStream(Stream instream)
{
MemoryStream outstream = new MemoryStream();
const int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = instream.Read(buffer, 0, bufferLen)) > 0)
{
outstream.Write(buffer, 0, count);
}
return outstream;
}

原文地址:https://www.cnblogs.com/roper/p/11804190.html

时间: 2024-11-13 05:51:54

下载网络文件,网络流转换为内存流的相关文章

Java Http下载网络文件

Java Http下载网络文件 使用URL.BufferedReader读取网络文件,转换为字符串,同时可以保存到本地文件. /** 从网络地址url下载文件读成字符串 * @param downloadUrl 文件的网络地址 * @return */ public static StringBuffer downloadFromUrl(String downloadUrl) { BufferedReader reader = null; StringBuffer stringBuffer =

Java BufferedReader下载网络文件

使用java BufferedReader从网络中读取文件到本地,可以存入数据库,也可以保存到本地 java代码 1.下载网络文件内容转换成StringBuffer /** 从网络地址url下载文件读成字符串 * @param downloadUrl 文件的网络地址 * @return */ public static StringBuffer downloadFromUrl(String downloadUrl) { BufferedReader reader = null; StringBu

C#限速下载网络文件

代码: using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.

网络编程(一):用C#下载网络文件的2种方法

使用C#下载一个Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient.具体处理起来还有同步和异步两种方式,所以我们其实有四种组合. 1.使用HttpWebRequest/HttpWebResonse和WebClient HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); WebResponse response = request.GetResponse();

如何将网络流转化为内存流 C#

//将获取的文件流转化为内存流 public static MemoryStream ConvertStreamToMemoryStream(Stream stream) { MemoryStream memoryStream = new MemoryStream(); if (stream != null) { byte[] buffer = ReadFull(stream); if (buffer != null) { var binaryWriter = new BinaryWriter(

Python下载网络文本数据到本地内存的四种实现方法示例

reference: https://blog.csdn.net/john_bian/article/details/71025372?utm_source=itdadao&utm_medium=referral import urllib.request import requests from io import StringIO import numpy as np import pandas as pd ''' 下载网络文件,并导入CSV文件作为numpy的矩阵 ''' # 网络数据文件

C#文件操作(IO流 摘抄)

11 文件操作概述 11.1 驱动器 在Windows操作系统中,存储介质统称为驱动器,硬盘由于可以划分为多个区域,每一个区域称为一个驱动器..NET Framework提供DriveInfo类和 DriveType枚举型,以方便在程序中直接使用驱动器.DriveInfo类的常用字段成员有DriveFormat(文件系统格式,如NTFS或FAT32).DriveType(驱动器类型).Name(驱动器名).TotalSize(总空间).TotalFreeSpace(获得驱动器可用空间).常用的方

C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)

一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件:复制代码 //文件流:读取 FileStream fileStream = File.Open(@"D:\test.txt", FileMode.Open);//初始化文件流 byte[] array = new byte[fileStream.Length];//初始化字节数组,用来暂存读取到的字节 fileStream.Read(array, 0, array.Length);/

用内存流 文件流 资源生成客户端(Delphi开源)

正文:很多木马生成器就是用的内存流和文件流生成客户端的,废话不多说了,代码如下: unit Main; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, Inifiles; typeTForm1 = class(TForm)GroupBox1: TGroupBox;msgtitle: TEdit;GroupBox2: TGroupBo