本帖最后由 ilovecomputer66 于 2023-1-20 18:47 编辑
是这样的,调用第三方平台的API,上传一个本地图片(会很大,几十MB,然后他们服务器处理后,返回下载处理后的图片)
想知道上传的进度(即已经上传多少MB),服务器处理完后,response返回处理后的文件时,也要知道下载进度
看网上有些例子,是还需要再添加包Microsoft.AspNet.WebApi.Client ,比如:https://blog.csdn.net/adai3846250/article/details/117159419
但我按它这个做,直接程序无法运行,会卡死,但不报错,不知道什么原因(下面button2的点击事件),如果不用文中说的ProgressMessageHandler,不监控进度,即button1我原来写的点击事件,就没问题
(我即便把ProgressMessageHandler_HttpSendProgress 2个函数,里面清空掉),还是会让整个程序卡死,不知道为什么
[C#] 纯文本查看 复制代码 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void SafeInvoke(Action action)
{
if (this.IsDisposed == false && this.Created == true)
{
this.Invoke(action);
}
}
private void button1_Click(object sender, EventArgs e)
{
HttpClientHandler handler = new HttpClientHandler();
handler.AllowAutoRedirect = true;
handler.CheckCertificateRevocationList = false;
handler.UseCookies = false;
HttpClient httpClient = new HttpClient(handler);
httpClient.Timeout = TimeSpan.FromMilliseconds(50000);
MultipartFormDataContent formData = new MultipartFormDataContent();
formData.Headers.Add("X-Api-Key", "hdNWthxGKPjxYMxwY1riGvBN"); // hdNWthxGKPjxYMxwY1riGvBN
formData.Add(new ByteArrayContent(File.ReadAllBytes(@"C:\Users\zhang\Desktop\input2.jpg")), "image_file", "input1.jpg");
formData.Add(new StringContent("auto"), "size");
var response = httpClient.PostAsync("https://api.remove.bg/v1.0/removebg", formData).Result;
if (response.IsSuccessStatusCode)
{
FileStream fileStream = new FileStream(@"C:\Users\zhang\Desktop\temp3.jpg", FileMode.Create, FileAccess.Write, FileShare.None);
response.Content.CopyToAsync(fileStream).ContinueWith((copyTask) => { fileStream.Close(); });
MessageBox.Show("ok");
}
else
{
MessageBox.Show("Error: " + response.Content.ReadAsStringAsync().Result);
}
}
private void button2_Click(object sender, EventArgs e)
{
HttpClientHandler handler = new HttpClientHandler();
handler.AllowAutoRedirect = true;
handler.CheckCertificateRevocationList = false;
handler.UseCookies = false;
ProgressMessageHandler progressMessageHandler = new ProgressMessageHandler(handler);
progressMessageHandler.HttpSendProgress += ProgressMessageHandler_HttpSendProgress;
progressMessageHandler.HttpReceiveProgress += ProgressMessageHandler_HttpReceiveProgress;
HttpClient httpClient = new HttpClient(progressMessageHandler);
httpClient.Timeout = TimeSpan.FromMilliseconds(50000);
MultipartFormDataContent formData = new MultipartFormDataContent();
formData.Headers.Add("X-Api-Key", "hdNWthxGKPjxYMxwY1riGvBN"); // hdNWthxGKPjxYMxwY1riGvBN
formData.Add(new ByteArrayContent(File.ReadAllBytes(@"C:\Users\zhang\Desktop\input3.jpg")), "image_file", "input1.jpg");
formData.Add(new StringContent("auto"), "size");
var response = httpClient.PostAsync("https://api.remove.bg/v1.0/removebg", formData).Result;
if (response.IsSuccessStatusCode)
{
FileStream fileStream = new FileStream(@"C:\Users\zhang\Desktop\temp3.jpg", FileMode.Create, FileAccess.Write, FileShare.None);
response.Content.CopyToAsync(fileStream).ContinueWith((copyTask) => { fileStream.Close(); });
MessageBox.Show("ok");
}
else
{
MessageBox.Show("Error: " + response.Content.ReadAsStringAsync().Result);
}
}
private void ProgressMessageHandler_HttpSendProgress(object sender, HttpProgressEventArgs e)
{
this.SafeInvoke(() =>
{
LblUpload.Text = e.ProgressPercentage + "%";
this.Refresh();
});
}
private void ProgressMessageHandler_HttpReceiveProgress(object sender, HttpProgressEventArgs e)
{
this.SafeInvoke(() =>
{
LblDownload.Text = e.ProgressPercentage + "%";
this.Refresh();
});
}
} |