UWP から WebAPI2 にデータ [ユーザー ID、ファイル] を送信しようとしています。WebApi2から、Azure Storageにコンテナを作成したい[コンテナ名=ユーザー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")] public class 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 に渡すには助けが必要です。ヘルプ/提案は大歓迎です。ありがとう :)
注意: 私は初心者です :)