Upload and download stream in Azure Blob

I have a simple application where I am making a form post with entering a file type into a WCF service. I am trying to download this file from a form post in Azure blob and then load it by clicking the corresponding blob url.

What happens now, I upload the file to Azure, but when I upload it, the file does not contain content. For example, if I download something.zip or something.gif, I can download it from the URL, but both of them will not contain anything.

I read that this may be due to the fact that the position of the stream is not set to 0. This will not allow me to set the position of the stream "Stream" below, so I copied it to memoryStream. Unfortunately, this did not solve the problem.

[OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Upload")] public string upload(Stream stream) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(exerciseContainer); CloudBlob blob = container.GetBlobReference("myblob"); Stream s = new MemoryStream(); stream.CopyTo(s); s.Seek(0, SeekOrigin.Begin); blob.UploadFromStream(s); return "uploaded"; } 

I also know that the file goes to the service through fiddler / breaks, and I successfully uploaded and uploaded the files using hard-coding in a file like this:

 using (var fileStream = System.IO.File.OpenRead(@"C:\file.zip")) { blob.UploadFromStream(fileStream); } 

Edit: HTML

 <form action='http://127.0.0.1:81/service.svc/Upload' method="post"> <input type="file" name="file" id="btnUpload" value="Upload" /> <input type="submit" value="Submit" class="btnExercise" id="btnSubmitExercise"/> </form> 
+4
source share
1 answer

Your code looks correct. But I have doubts about the flow you receive. Are you sure it contains data? Could you try the following code? It returns the total number of bytes received after blob loading.

 using (var ms = new MemoryStream()) { byte[] buffer = new byte[32768]; int bytesRead, totalBytesRead = 0; do { bytesRead = stream.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; ms.Write(buffer, 0, bytesRead); } while (bytesRead > 0); blob.UploadFromStream(ms); return String.Format("Uploaded {0}KB", totalBytesRead/1024); } 
+3
source

Source: https://habr.com/ru/post/1438926/


All Articles