gpt4 book ai didi

asp.net - 文件将文件上传到 Azure Blob 存储

转载 作者:行者123 更新时间:2023-12-02 07:48:51 25 4
gpt4 key购买 nike

我有一个 System.Web.UI.WebControls.FileUpload 控件,用于传递需要存储在 Azure Blob 存储中的 Word 和 PDF 文件。

从代码隐藏页面传递到公共(public)库来管理 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

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

我的问题:

  1. 这是获取已上传文件并将其保存到 Azure Blob 存储的最佳方式吗?
  2. 我是否正确处理流?我正在传递 ByRef 并有一个关于用法的 using 语句。
  3. 将内容保存到存储时是否应该明确设置内容类型?如果是这样我该怎么做?

注意,我通常使用 C# 进行编码,因此如果您不熟悉 VB.NET,可以使用 C# 示例。

最佳答案

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

最好的方法取决于您的用例。如果只是小文件就可以了。如果您想支持大文件,您可能需要进行分块上传。您可以获取 1 MB 的 block ,并可以单独或并行上传。上传完所有 block 后,您将提交该文件,并将其拼接到 Azure Blob 存储中。看CloudBlockBlob.PutBlockCloudBlockBlob.PutBlockList

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

是的,但是如果您想支持更大的文件,您可能需要使用 JavaScript 上传并创建两个端点来接收 block 并在发送所有 block 后提交。有多个库可以为您提供帮助。

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

如果您上传想要嵌入 HTML 的文件,那么最好使用内容类型。如果您希望文件的链接成为下载链接,则不必这样做。虽然它永远不会伤害。

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

关于asp.net - 文件将文件上传到 Azure Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46027690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com