csharp 文件流api到本地文件保存

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 文件流api到本地文件保存相关的知识,希望对你有一定的参考价值。

FileStream writeStream = new FileStream(strDocumentName, FileMode.Create, FileAccess.Write);
ReadWriteStream(readStream, writeStream);

// save excel to file
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
	int Length = 256;
	Byte[] buffer = new Byte[Length];
	int bytesRead = readStream.Read(buffer, 0, Length);

	while (bytesRead > 0)
	{
		writeStream.Write(buffer, 0, bytesRead);
		bytesRead = readStream.Read(buffer, 0, Length);
	}
	readStream.Close();
	writeStream.Close();
}

// api consume return stream excel
private Stream ObtReportDocument()
{
	if (ProcessSession["INFORME"].ToString().Equals("RES"))
	{
		string json = LoadInfR();

		string baseAddress = "http://filegenerator-api-lb.test.BDP.local/api/excel";

		HttpWebRequest http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
		http.Accept = "application/json";
		http.ContentType = "application/json";
		http.Method = "POST";

		ASCIIEncoding encoding = new ASCIIEncoding();
		Byte[] bytes = encoding.GetBytes(json);

		Stream newStream = http.GetRequestStream();
		newStream.Write(bytes, 0, bytes.Length);
		newStream.Close();

		WebResponse response = http.GetResponse();

		Stream stream = response.GetResponseStream();

		return stream;
	}

	return null;
}

以上是关于csharp 文件流api到本地文件保存的主要内容,如果未能解决你的问题,请参考以下文章

如何将 fastify swagger api 描述保存到本地文件

有啥方法可以将 API 响应作为文件保存到本地计算机磁盘

Objective C/IOS - 将音频流保存到文件

将 Web 音频 API 输出保存到 Ionic 中的本地文件

js怎么把id存到浏览器本地

下载远程(第三方服务器)文件图片,保存到本地(服务器)的方法保存抓取远程文件图片