ASP.NET Web页面将文件上载到Azure存储

时间:2016-03-02 14:14:07

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

有一些关于文件上传到ASP.NET的MVF的参考,但我在ASP.NET网页的字段中找不到

如何实现此类代码上传到Azure存储?

嗯..欲了解更多信息,

我的目标是在CK编辑器中上传图片

但由于Azure Hosting,普通的CKEditor引用无效。

所以我用Google搜索并使用此代码块

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 WebPages上传到Azure存储?

P.S&GT;更清楚的是,我的上传.aspx&#39;源文件是这个

upload.aspx

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我已经解决了自己!通过使用Visual Studio,我发现了一个问题。 2009东海生日贺!!

即使我不喜欢Visual Studio,Visual Studio也是非常强大的工具

也许它的重量是值得的。

此代码可以使用!

<%@ 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);
    } 

 .....