我有一个ASP.Net MVC项目,我需要异步上传文件到RackSpace CloudFiles或者在api中使用Queue功能,有什么例子吗?我没有在openstack.net文档和C#代码示例中找到它。
答案 0 :(得分:1)
在撰写本文时,没有.net库为Rackspace Cloud Files提供异步文件上传。 但是,有一个承诺,即openstack.net的V2将提供异步支持。
您现在可以使用HttpClient:
public static async Task<bool> UploadFileAsync(string filepath, string container, string filename )
{
var httpClient = new HttpClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Post,
@"https://identity.api.rackspacecloud.com/v2.0/tokens")
{
Content = new StringContent(
@"{
""auth"": {
""RAX-KSKEY:apiKeyCredentials"": {
""username"": ""username"",
""apiKey"": ""apikey""
}
}
}")
};
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await httpClient.SendAsync(requestMessage);
var responseContent = await response.Content.ReadAsStringAsync();
var obj = JObject.Parse(responseContent);
var tokenId = obj.SelectToken("access.token.id").ToObject<string>();
var endpointPublicURL = obj.SelectToken("access.serviceCatalog[?(@.name == 'cloudFiles')].endpoints[0].publicURL");
var fileBytes = File.ReadAllBytes(filepath);
using (var httpContent = new ByteArrayContent(fileBytes))
{
httpContent.Headers.Add("X-Auth-Token", tokenId);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
httpContent.Headers.ContentLength = fileBytes.LongLength;
var result = await httpClient.PutAsync(endpointPublicURL + "/" + container + "/" filename, httpContent);
}
return true;
}
答案 1 :(得分:0)
穆罕默德 -
虽然不是异步,但我确实有一个MVC 4示例,展示了如何在上传过程中显示进度条。
这是今天早上发生的。你可以在这里找到它:
https://github.com/DonSchenck/ProgressBarMVC4
如果这有帮助,或者如果您需要更多,请告诉我。
All The Best,
- Don Schenck,OpenStack.NET开发人员倡导者,Rackspace