简体   繁体   English

FileUpload文件到Azure Blob存储

[英]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. 我有一个System.Web.UI.WebControls.FileUpload控件,该控件传递需要存储在Azure Blob存储中的Word和PDF文件。

From the Code Behind page it passes to the common library to manage Azure Functions: 它从“代码隐藏”页面传递到公共库以管理Azure函数:

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: AzureStorage.SaveBlob代码:

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? 这是获取已上传文件并将其保存到Azure Blob存储的最佳方法吗?
  2. Am I handling the Stream correctly? 我是否正确处理了Stream? I'm passing ByRef and have a Using statement around the usage. 我正在传递ByRef,并在用法周围有一个Using语句。
  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. 注意 ,我通常使用C#编写代码,因此如果您不熟悉VB.NET,则可以使用C#的示例。

Is this best way to take the File that has been uploaded and save it to Azure Blog Storage? 这是获取已上传文件并将其保存到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. 您可以占用1兆字节的块,可以分别上传或并行上传。 Once you are done uploading all the blocks you commit the file and it is stiched together in Azure Blob storage. 完成所有块的上传后,您将提交文件,并将其在Azure Blob存储中一起存储。 Look at CloudBlockBlob.PutBlock and CloudBlockBlob.PutBlockList . 查看CloudBlockBlob.PutBlockCloudBlockBlob.PutBlockList

Am I handling the Stream correctly? 我是否正确处理了Stream? I'm passing ByRef and have a Using statement around the usage. 我正在传递ByRef,并在用法周围有一个Using语句。

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. 您可以,但是如果您想支持更大的文件,则可能需要使用JavaScript上载并创建两个端点来接收块并在发送所有块之后提交。 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. 如果您要上传要嵌入HTML的文件,则最好使用内容类型。 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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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