So, it seems to me that the code that I posted above actually saves the local file and only then uploads it to the server, which causes an error, but also slower. after many attempts, I finally move on to the next solution, and it all started, and it was even faster!
first create a streamprovider:
public class BlobStorageMultipartStreamProvider : MultipartStreamProvider { private readonly string _containerName; private readonly string _fileName; public BlobStorageMultipartStreamProvider(string containerName, string fileName) { _containerName = containerName; _fileName = fileName; } public override Stream GetStream(HttpContent parent, HttpContentHeaders headers) { Stream stream = null; if (!String.IsNullOrWhiteSpace(_fileName)) { string connectionString = ConfigurationManager.ConnectionStrings["BlobStorage"].ConnectionString; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(_containerName); CloudBlockBlob blob = blobContainer.GetBlockBlobReference(_fileName); stream = blob.OpenWrite(); } return stream; } }
Download Code:
string fileName = Guid.NewGuid()+".Png"; MultipartStreamProvider provider = new BlobStorageMultipartStreamProvider("container",fileName); Trace.TraceInformation("Uploading raw image to blob"); await Request.Content.ReadAsMultipartAsync(provider); Trace.TraceInformation("Uploading finished");
source share