[UWP] [C ++] HttpClient PostAsync多个文件并避免超时 [英] [UWP][C++] HttpClient PostAsync multiple files and avoid timeouts

查看:186
本文介绍了[UWP] [C ++] HttpClient PostAsync多个文件并避免超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传图像到服务器(00或000)。用户首先选择一个文件夹,输入相关信息并点击上传。

I have an up that uploads images to a server (00's or 000's). The user first selects a folder, enters the relevant information and clicks upload.

在我的上传功能中,我发现所选文件夹中的所有图像文件并逐个上传。代码看起来像这样......

In my upload function I discover all the image files in the selected folder and upload one by one. The code looks like this...

auto query = _upload_dir->CreateFileQueryWithOptions(Q);

create_task(query->GetFilesAsync()).then([=](IVectorView<StorageFile^>^ imgs) {
	Uri^ url = ref new Uri(UploadImageSecure_URL());
	
	for (unsigned int i = 0; i < imgs->Size; i++)
	{
		Windows::Web::Http::HttpClient^ httpClient = ref new HttpClient(filter);
		concurrency::cancellation_token_source cancellationTokenSource = cancellation_token_source();
		concurrency::task<IRandomAccessStream^> AsyncOpen_task;
		cancellation_token_source setImageTokenSource;

		IAsyncOperation<IRandomAccessStream^>^ AsyncOpen = imgs->GetAt(i)->OpenAsync(Windows::Storage::FileAccessMode::Read);
		AsyncOpen_task = create_task(AsyncOpen, setImageTokenSource.get_token());

		//open the image stream
		AsyncOpen_task.then([this, httpClient, url, setImageTokenSource, imgs, i, json_msg](IRandomAccessStream^ stream) {
			HttpStreamContent^ streamContent = ref new HttpStreamContent(stream);
			HttpMultipartFormDataContent^ form = ref new HttpMultipartFormDataContent();
			form->Add(streamContent, "file", imgs->GetAt(i)->Name);
			form->Add(ref new HttpStringContent(json_msg), "upload_info");

			return create_task(httpClient->PostAsync(url, form), setImageTokenSource.get_token());
		}, task_continuation_context::use_current()).then([this](HttpResponseMessage^ response)
		{
			return response->Content->ReadAsStringAsync();
		}, task_continuation_context::use_current()).then([=](task<String^> previousTask)
		{
			try
			{
				// Check if any previous task threw an exception.
				previousTask.get();
				uploadCount++;
				double progress = 100 * double(uploadCount) / (double)imgs->Size;

				UpdateProgressBars(progress);

				if (uploadCount == imgs->Size) {
					HideProgressBars();
				}
			}
			catch (const task_canceled&)
			{
			}
			catch (Exception^ ex)
			{
				//fail
			}
		}, task_continuation_context::use_current());
	}
});

我遇到的问题是当图片太多或者数量较少但上传的尺寸较大时(受到连接速度的限制)文件将超时结束 

The issue I am having is when there are too many images or there are fewer but they are larger in size the upload (being limited by the connection speed) will timeout for files towards the end of 

IVectorView<StorageFile^>^ imgs

我真正需要的是不PostAsync,我只想在上一个图像完成时开始上传下一个图像。当唯一的选项是PostAsync时,如何才能做到这一点?我已经尝试过  AsyncOpen_task.get();和&NBSP; AsyncOpen_task.wait();
但我无法弄清楚如何使用它们。

What I really need is to NOT PostAsync, I want to only start uploading the next image when the previous one finishes. How can this be done when the only option is PostAsync? I've experimented with AsyncOpen_task.get(); and AsyncOpen_task.wait(); but I can't figure out how to use them.

推荐答案

您好,

对于大量数据使用  后台转移,特别是文件上传使用  BackgroundUploader  。

For large amount of data use Background transfers , especially for file uploading use BackgroundUploader .


这篇关于[UWP] [C ++] HttpClient PostAsync多个文件并避免超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆