简体   繁体   中英

FileUpload file to Azure Blob Storage

I have a System.Web.UI.WebControls.FileUpload control that passes both Word and PDF files that need to be stored in Azure Blob Storage.

From the Code Behind page it passes to the common library to manage Azure Functions:

Private Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UploadButton.Click
    Dim fileExt As String = String.Empty
        Dim newGuid As New Guid
        Dim fileName As String
        Dim documentType As Document.DocumentType

        Page.Validate()

        newGuid = Guid.NewGuid

        If Page.IsValid() AndAlso Me.FileUploadNewDoc.HasFile Then
            Try
                    'Test that MIME is either msword of pdf
                        If FileUploadNewDoc.PostedFile.ContentType.Contains("msword") Then
                            fileExt = "doc"
                            documentType = Document.DocumentType.LeaseWordDoc
                        ElseIf FileUploadNewDoc.PostedFile.ContentType.Contains("pdf") Then
                            fileExt = "pdf"
                            documentType = Document.DocumentType.LeasePDF
                        Else
                            fileExt = "na"
                        End If

                        If fileExt <> "na" Then
                            fileName = newGuid.ToString & "." & fileExt
                            AzureStorage.SaveBlob(FileUploadNewDoc.FileContent, fileName, mDocumentContainer, mStorageConnectionString)

                        End If
                    Catch ex As Exception
                        ' Handle Error
                    Finally
                        FileUploadNewDoc.Dispose()
                    End Try
    End If
End Sub

The AzureStorage.SaveBlob code:

Public Function SaveBlob(ByRef fileContent As Stream,
                                 ByVal fileName As String, 
                                 ByVal containerName As String, 
                                 ByVal storageConnectionString As String) As Boolean

            Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(storageConnectionString)
            Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
            Dim container As CloudBlobContainer = blobClient.GetContainerReference(containerName)
            Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(fileName)

            Using fileContent
                fileContent.Position = 0
                blockBlob.UploadFromStream(fileContent)
            End Using

            Return True
End Function

My questions:

  1. Is this best way to take the File that has been uploaded and save it to Azure Blob Storage?
  2. Am I handling the Stream correctly? I'm passing ByRef and have a Using statement around the usage.
  3. Should I be setting content type explicitly when saving it to storage? If so how do I do that?

Note , I normally code in C# so an example in C# is fine if you're not familiar with VB.NET.

Is this best way to take the File that has been uploaded and save it to Azure Blog Storage?

The best way depends on your use case. If it is just small files you're OK. If you want to support large files you might want to do chunked uploading. You can take blocks of 1 megabyte which you can upload separately or in parallel. Once you are done uploading all the blocks you commit the file and it is stiched together in Azure Blob storage. Look at CloudBlockBlob.PutBlock and CloudBlockBlob.PutBlockList .

Am I handling the Stream correctly? I'm passing ByRef and have a Using statement around the usage.

You are but if you want to support larger files you might want to upload with JavaScript and create two endpoint to receive chunks and to commit after all chunks are sent. There are multiple libraries that can help you.

Should I be setting content type explicitly when saving it to storage? If so how do I do that?

If you upload files that you want to embed in HTML it's wise to have a content type. If you want the links to the file to be download links you don't have to. Although it can never hurt.

blockBlob.Properties.ContentType = "image/jpeg";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM