【发布时间】:2017-12-09 23:34:27
【问题描述】:
我试图通过 UWP c# 使用文件流将文件上传到 Web 服务器,但它总是给我一个错误,即当我尝试在 http://example.com/httpdocs/content 上上传时不允许使用 405 方法。即使出于测试目的,我也尝试在本地主机上上传,但仍然没有运气。 有什么帮助吗?
代码:
public async Task<bool> Upload(StorageFile fileName)
{
HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
cts = new CancellationTokenSource();
using (IInputStream fileStream = await fileName.OpenSequentialReadAsync())
{
HttpStreamContent content = new HttpStreamContent(fileStream);
form.Add(content, "premier", fileName.Name);
using (HttpClient client = new HttpClient())
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("http://example.com/httpdocs/content")))
{
request.Content = form;
request.Headers.TryAppendWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendRequestAsync(request).AsTask(cts.Token);
var result = response.Content.ReadAsStringAsync().GetResults();
}
}
}
return true;
}
【问题讨论】:
-
我们可以看看您是如何尝试使用
FileStream上传文件的吗? -
好吧,首先你为什么要上传到那个ulr?方法不允许是因为你不能向它发出
POST请求 -
因为它实际上需要将 Excel 文件保存到 Web 服务器。我们不能上传这样的文件吗?
-
我们通常使用上传控件或在Web服务器中添加服务来上传文件。
-
@LeiYang 上传 HttpClient 或 BackgroundUpload 之类的控件?
标签: c# asp.net sql-server xaml uwp