【问题标题】:upload files to Azure file storage from web app using rest api使用 rest api 从 Web 应用程序将文件上传到 Azure 文件存储
【发布时间】:2016-10-15 23:16:52
【问题描述】:

我有一个 Web 应用程序,当前使用的是 webforms,而不是 MVC,它将托管在 Azure 平台上。

这个网页应用的主要功能是将用户文件上传到Azure File Storage。

文件可能是pdf、mp3等,不是简单的文本或数据流或数据输入。

有人告诉我使用 Azure REST API 来上传文件,但我真的不熟悉它,在网上找不到好的示例或教程或视频。微软的当前文件看起来像??????对我来说。

目前我只是上传到本地文件夹,所以代码如下: FileUpload1.PostedFile.SaveAs(Server.MapPath("fileupload\\" + FileUpload1.FileName)); 在 C# 中;

我从那里去哪里?我想我应该添加一个看起来像 DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy 的 StorageConnectionString,我已经有了。

然后我应该在 C# 中编写一些类似 'post' 的代码?

【问题讨论】:

    标签: c# asp.net rest azure-storage azure-web-app-service


    【解决方案1】:

    Azure 提供了一个 nuget 库,您可以使用它在 Azure 文件存储上上传和执行其他“文件管理”类型的活动。

    该库被称为: WindowsAzure.Storage

    更新:要使用的新库是Azure.Storage.Blobs

    以下是实现这一目标的基础:

    //Connect to Azure
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
    
    // Create a reference to the file client.
    CloudFileClient fileClient = storageAccount.CreateCloudFileClient();      
    
    // Create a reference to the Azure path
    CloudFileDirectory cloudFileDirectory = GetCloudFileShare().GetRootDirectoryReference().GetDirectoryReference(path);
    
    //Create a reference to the filename that you will be uploading
    CloudFile cloudFile = cloudSubDirectory.GetFileReference(fileName);
    
    //Open a stream from a local file.
    Stream fileStream= File.OpenRead(localfile);
    
    //Upload the file to Azure.
    await cloudFile.UploadFromStreamAsync(fileStream);
    fileStream.Dispose();
    

    更多链接和信息在这里(注意向下滚动以获取示例):https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/

    【讨论】:

    • 天哪,我哭了,这很有帮助,谢谢!拯救我的一天。解决了大部分问题!
    • 这就是我们使用“等待”的原因吗?有必要吗?当我使用它时,错误提示“await 运算符只能在异步方法中使用”,当我将异步添加到“受保护的无效 Button1_Click”(这是上传文件的按钮单击)时,将其更改为“受保护的异步无效” ,另外一个错误会显示为“An asynchronous operation cannot be started at this time”……我惭愧我不懂这么多东西(T.T)
    • UploadFromStreamAsync 是一种异步方法。所以你要么需要把它放在一个带有异步static async void UploadFile()的方法中,要么使用Wait()函数让它同步。 cloudFile.UploadFromStreamAsync(fileStream).Wait();。祝你好运。
    • 谢谢,经过一番努力,我终于解决了这个问题,我将在下面附上我的最终代码,谢谢你的帮助!我希望这个问题能帮助那些有类似问题的人。
    • @GaryHolland 有没有其他方法可以直接将文件上传到云目录,而不是在目录中创建文件的引用并进行异步复制?
    【解决方案2】:

    这段代码基于我从上面的 Gary Holland 那里得到的答案。我希望其他人从中受益。我不擅长编程,希望代码看起来没问题。

    if (FileUpload1.HasFile)
        {
            //Connect to Azure
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
    
            // Create a reference to the file client.
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    
            // Get a reference to the file share we created previously.
            CloudFileShare share = fileClient.GetShareReference("yourfilesharename");
    
            if (share.Exists())
            {
    
    
                // Generate a SAS for a file in the share
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("folderthatyouuploadto");
                CloudFile file = sampleDir.GetFileReference(FileUpload1.FileName);
    
                Stream fileStream = FileUpload1.PostedFile.InputStream;
    
                file.UploadFromStream(fileStream);
                fileStream.Dispose();
    
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2017-07-10
      • 1970-01-01
      • 2023-02-01
      • 2018-03-15
      相关资源
      最近更新 更多