【问题标题】:File Uploading to Azure Storage by ASP.NET Webpages通过 ASP.NET 网页将文件上传到 Azure 存储
【发布时间】:2016-03-05 01:51:03
【问题描述】:

有一些关于通过 ASP.NET MVC 将文件上传到 Azure 的参考,但我在 ASP.NET 网页字段中找不到任何参考

如何实现这样的代码上传到 Azure 存储?

嗯.. 欲了解更多信息,

我的目标是在 CK Editor 中上传图片

但由于 Azure 托管,普通的 CKEditor 参考不起作用。

所以我用谷歌搜索并使用了这个代码块

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("lawimage");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(name))
{
    blockBlob.UploadFromStream(fileStream);
}

但它不起作用,

我的“web.config”是

<appSettings>
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=lawcbt;AccountKey=[MyAccountKey]/>
</appSettings>

有没有人通过 ASP.NET 网页上传到 Azure 存储?

P.S>为了更清楚,我的'upload.aspx'源文件是这个

upload.aspx

【问题讨论】:

  • 您是否遇到某种处理错误?还是对服务器的 POST 请求返回 OK (200) 但文件不在容器上?
  • 处理错误,我想要一些完整的上传到 Azure 源代码是由 Asp.net 网页编写的

标签: asp.net azure ckeditor asp.net-webpages


【解决方案1】:

当然,您必须先将文件上传到您的 Asp.Net 网页。从那里您可以将其上传到您的 blobstorage。在您的代码中,您似乎正在尝试将文件从服务器上传到 blobstorage。首先您必须将文件上传到服务器,然后您可以将流发送到 blobstorage。

【讨论】:

  • 好吧.. 问题是“将流发送到 blobstorage”不起作用。我想知道一些ASP.net网页写的源代码
【解决方案2】:

我自己解决了!通过使用 Visual Studio,我发现了一个漏洞。呵呵!!

虽然我不喜欢 Visual Studio,但 Visual Studio 是非常强大的工具 Tongue Out

也许它的沉重是值得的。

此代码将起作用!

<%@ Import namespace="System.Configuration" %>
<%@ Import namespace="Microsoft.WindowsAzure" %>
<%@ Import namespace="Microsoft.WindowsAzure.Storage" %>
<%@ Import namespace="Microsoft.WindowsAzure.Storage.Auth" %>
<%@ Import namespace="Microsoft.WindowsAzure.Storage.Blob" %>

......

    HttpPostedFile theFile = HttpContext.Current.Request.Files[0];
    // Azure Upload 

    // Retrieve storage account from connection string.
    StorageCredentials sc = new StorageCredentials("[MyStorageName]", "[MyKey]");
    CloudStorageAccount storageAccount = new CloudStorageAccount(sc, false);

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference("lawimage");

    // Retrieve reference to a blob named "myblob".
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(sFileName);

    // Create or overwrite the "myblob" blob with contents from a local file.
    using (var fileStream = theFile.InputStream)
    {
        blockBlob.UploadFromStream(fileStream);
    } 

 .....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多