我正在尝试将数据[用户ID,文件]从UWP发送到WebAPI2。从WebApi2,我想在Azure存储中创建一个容器[容器名称=用户ID]。代码如下。第一部分是客户端[UWP]。
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<p:panel
header="panel-1"
id="panel-1"
toggleable="true"
collapsed="true">
</p:panel>
<p:panel
header="panel-2"
id="panel-2"
toggleable="true"
collapsed="true">
</p:panel>
</ui:composition>
以下代码表示在Azure Blob中上传文件的WebAPI2代码:
foreach (StorageFile file in files)
{
filesListView.Items.Add(file.DisplayName);
IInputStream inputStream = await file.OpenAsync(FileAccessMode.Read);
HttpMultipartFormDataContent multipartContent = new HttpMultipartFormDataContent();
//if (i == false)
//{
multipartContent.Add(new HttpStringContent(txtbox.Text), "myText");
i = true;
// }
multipartContent.Add(new HttpStreamContent(inputStream), "myFile", file.Name);
HttpClient client = new HttpClient();
Uri uri = new Uri("http://localhost:35095/api/upload");
HttpResponseMessage response = await client.PostAsync(uri,multipartContent);
}
{ [RoutePrefix(&#34; API /上传&#34)] public class UploadController:ApiController {
namespace DemoAzureStorage.Controllers
}
在上面的WebApi2代码中,正在创建Azure存储容器,但是当我调用以下方法时,会创建blob但是使用&#34; 0字节大小&#34;即,没有数据/内容传递给blob,而只是创建了具有相应文件名的blob:
[HttpPost, Route("")]
public async Task<object> UploadFile()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
CloudBlobContainer imagesContainer = null;
string Container;
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
await Request.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider()).ContinueWith((tsk) =>
{
MultipartMemoryStreamProvider prvdr = tsk.Result;
var accountName = ConfigurationManager.AppSettings["storage:account:name"];
var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
foreach (HttpContent ctnt in prvdr.Contents)
{
// You would get hold of the inner memory stream here
Stream stream = ctnt.ReadAsStreamAsync().Result;
var type1 = ctnt.GetType();
if (ctnt.Headers.ContentDisposition.Name == "\"myFile\"")
{
var filename = ctnt.Headers.ContentDisposition.FileName.Trim();
var a = filename;
CloudBlockBlob blob = imagesContainer.GetBlockBlobReference(filename);
if (ctnt.Headers.ContentType != null)
{
// Set appropriate content type for your uploaded file
blob.Properties.ContentType = ctnt.Headers.ContentType.MediaType;
}
// this.FileData.Add(new MultipartFileData(ctnt.Headers, blob.Name));
StreamReader rdr = new StreamReader(stream);
var a1 = rdr;
blob.UploadFromStream(stream);
}
else
{
string aaa = ctnt.Headers.ContentDisposition.ToString();
StreamReader rdr = new StreamReader(stream);
Container = rdr.ReadLine();
string z = Container;
imagesContainer = blobClient.GetContainerReference(z);
imagesContainer.CreateIfNotExists();
while (!rdr.EndOfStream)
{
// Console.WriteLine(rdr.ReadLine());
Trace.WriteLine(rdr.ReadLine());
}
}
}
});
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}
需要帮助将文本文件的数据原样传递给blob。任何帮助/建议将受到高度赞赏。谢谢:))
N.B:我是新手:)