using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace IStation.Untity
{
///
/// http文件辅助类
///
public class HttpFileHelper
{
///
/// 上传文件
///
public static string Upload(string url, string filePath)
{
if (string.IsNullOrEmpty(url))
return default;
if (string.IsNullOrEmpty(filePath))
return default;
if (!System.IO.File.Exists(filePath))
return default;
try
{
var fileInfo = new System.IO.FileInfo(filePath);
string boundary = "----" + DateTime.Now.Ticks.ToString("x");// 边界符
byte[] beginBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n"); // 边界符开始。【☆】右侧必须要有 \r\n 。
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); // 边界符结束。【☆】两侧必须要有 --\r\n 。
var fileFormTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n";//文件模板
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.Method = WebRequestMethods.Http.Post;
request.AllowWriteStreamBuffering = false;
//request.KeepAlive = true;
request.Timeout = -1;
long contentLength = 0;
var fileFormItem = string.Format(fileFormTemplate, "file", fileInfo.Extension);
byte[] fileFormItemBytes = Encoding.UTF8.GetBytes(fileFormItem);
contentLength += beginBoundaryBytes.Length;
contentLength += fileFormItemBytes.Length;
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
contentLength += fileStream.Length;
contentLength += endBoundaryBytes.Length;
request.ContentLength = contentLength;
var requestStream = request.GetRequestStream();
requestStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
requestStream.Write(fileFormItemBytes, 0, fileFormItemBytes.Length);
//每次上传4M
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
fileStream.Close();
requestStream.Close();
var response = request.GetResponse();
string responsetext = string.Empty;
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
responsetext = reader.ReadToEnd();
}
return responsetext;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 文件上传
///
public static string Upload(string url, Stream stream, string suffix)
{
if (string.IsNullOrEmpty(url))
return default;
if (stream == null)
return default;
try
{
string boundary = "----" + DateTime.Now.Ticks.ToString("x");// 边界符
byte[] beginBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n"); // 边界符开始。【☆】右侧必须要有 \r\n 。
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); // 边界符结束。【☆】两侧必须要有 --\r\n 。
var fileFormTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n";//文件模板
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.Method = WebRequestMethods.Http.Post;
request.AllowWriteStreamBuffering = false;
//request.KeepAlive = true;
request.Timeout = -1;
long contentLength = 0;
var fileFormItem = string.Format(fileFormTemplate, "file", suffix);
byte[] fileFormItemBytes = Encoding.UTF8.GetBytes(fileFormItem);
contentLength += beginBoundaryBytes.Length;
contentLength += fileFormItemBytes.Length;
//var fileStream = new FileStream(steam, FileMode.Open, FileAccess.Read);
contentLength += stream.Length;
contentLength += endBoundaryBytes.Length;
request.ContentLength = contentLength;
var requestStream = request.GetRequestStream();
requestStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
requestStream.Write(fileFormItemBytes, 0, fileFormItemBytes.Length);
//每次上传4M
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)stream.Length))];
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
stream.Close();
requestStream.Close();
var response = request.GetResponse();
string responsetext = string.Empty;
using (Stream rs = response.GetResponseStream())
using (StreamReader reader = new StreamReader(rs))
{
responsetext = reader.ReadToEnd();
}
return responsetext;
}
catch (System.Exception ex)
{
throw ex;
}
}
///
/// 下载文件
///
public static Stream Download(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
//request.Timeout = 10 * 1000;//请求超时时间
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.NoContent)
return null;
var ms = new MemoryStream();
response.GetResponseStream().CopyTo(ms);
return ms;
}
}
///
/// 下载文件
///
public static void DownloadUri(string uri, string filePath)
{
var client = new System.Net.WebClient();
client.DownloadFile(uri, filePath);
}
}
}