You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.2 KiB
59 lines
2.2 KiB
1 year ago
|
using Newtonsoft.Json.Linq;
|
||
|
using Newtonsoft.Json;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Net.Http;
|
||
|
using System.Net.Http.Headers;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
using CPF.Controls;
|
||
|
|
||
|
namespace AksWebBrowser.Common
|
||
|
{
|
||
|
public class ChunkedUpload
|
||
|
{
|
||
|
private readonly HttpClient _httpClient;
|
||
|
|
||
|
public ChunkedUpload(HttpClient httpClient)
|
||
|
{
|
||
|
_httpClient = httpClient;
|
||
|
}
|
||
|
|
||
|
public async Task<string> UploadFileAsync(string url, string filePath)
|
||
|
{
|
||
|
string ret = string.Empty;
|
||
|
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
||
|
{
|
||
|
FileInfo fileInfo = new FileInfo(filePath);
|
||
|
int totalParts = 1;
|
||
|
int chunkNumber = 1;
|
||
|
// 读取文件流其实位置
|
||
|
var fileStreamPos = 0;
|
||
|
var uploadUrl = $"{url}?partNumber={chunkNumber}&chunks={totalParts}&size={fileInfo.Length}&start={fileStreamPos}&end={fileInfo.Length}&total={fileInfo.Length}&FileName={Path.GetFileName(filePath)}";
|
||
|
|
||
|
using (var client = new HttpClient())
|
||
|
{
|
||
|
|
||
|
var formData = new MultipartFormDataContent();
|
||
|
formData.Add(new StreamContent(fileStream, (int)fileStream.Length), "file", Path.GetFileName(filePath) + ".partNumber-1");
|
||
|
var response = await client.PostAsync(uploadUrl, formData);
|
||
|
var responseString = await response.Content.ReadAsStringAsync();
|
||
|
fileStream.Close();
|
||
|
fileStream.Dispose();
|
||
|
ret = responseString;
|
||
|
JObject jo = (JObject)JsonConvert.DeserializeObject(ret);
|
||
|
if (Convert.ToBoolean(jo["IsSucceed"].ToString()) == true)
|
||
|
{
|
||
|
string result = jo["result"].ToString();
|
||
|
JObject jo1 = (JObject)JsonConvert.DeserializeObject(result);
|
||
|
ret = jo1["url"].ToString();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
}
|
||
|
}
|