簡體   English   中英

從ASP.NET C#與MS Azure上的Blob存儲交互

[英]interacting with blob storage on MS Azure from ASP.NET C#

我在一個網站上工作,完成開發后應將其部署在Azure雲上,問題是我的Web應用程序應將用戶本地計算機上的Blob上傳到Blob存儲,我的問題是,如何獲取文件路徑文件? 我使用上載控件使用戶能夠在用戶單擊“提交”按鈕時上載文件,並且執行了將上載控件中指定的文件上載到Blob存儲區的代碼,如何在代碼中獲取filepath變量的值? 是否需要將文件上傳到托管我的Web應用程序的服務器上,或者我可以只使用客戶端本地計算機上文件的路徑? 這是上傳網絡表單代碼:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure;

namespace myProject
{
    public partial class popout : System.Web.UI.Page
    {


        protected void Button1_Click(object sender, EventArgs e)
        {
            string filePath;
            if (FileUpload1.PostedFile != null)
            {
                filePath = FileUpload1.PostedFile.FileName;
            }
            else { filePath = null; }

            // file name with path of the file uploaded using FileUpload1 control
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("connectionstringWhatever"));
                // Create a blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                // Get a reference to a container named “myContainer”
                CloudBlobContainer container = blobClient.GetContainerReference("myContainer");
                //setting the permission to public
                container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });


                // Retrieve reference to a blob named  "blob1".or if doesn't exist, create one
                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
            if (filePath != null)
            {
                using (var fileStream = System.IO.File.OpenRead(filePath))
                {

                    blockBlob.UploadFromStream(fileStream);
                }
            }
            else {
                string msg = "FileUpload1 = null";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');", true);

            }
        }
        }
    }

看一下MSDN上的此頁面,特別是示例:

https://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.fileupload.postedfile(v=vs.110).aspx

這與您的情況類似。 該示例將上載的文件保存到服務器,然后在其上執行其他操作。 但是,只要文件數據在內存中,就可以使用PostFile屬性將文件數據寫入Blob。

您將無法提供用戶計算機上文件的文件路徑。 上傳路徑僅通過上傳對話框提供; 您將無法從應用程序訪問該路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM