我正在尝试将数据 [用户 ID,文件] 从 UWP 发送到 WebAPI2。从 WebApi2,我想在 Azure 存储中创建一个容器 [容器名称 = 用户 ID]。代码如下。第一部分是客户端 [UWP]。
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);
}
下面的代码表示在 Azure Blob 中上传文件的 WebAPI2 代码:
namespace DemoAzureStorage.Controllers
{ [RoutePrefix("api/upload")] 公共类 UploadController : ApiController {
[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);
}
}
}
}
在上面的 WebApi2 代码中,正在创建 Azure 存储容器,但是当我调用以下方法时,会创建 Blob,但具有“0 字节大小”,即没有数据/内容被传递给 Blob,而只创建了一个 Blob相应的文件名:
blob.UploadFromStream(stream);
需要帮助将文本文件的数据按原样传递给 blob。任何帮助/建议将不胜感激。谢谢 :)
注意:我是新手 :)