File Uploading to Azure Storage by ASP.NET Webpages

拜拜、爱过 提交于 2019-12-08 11:14:49

问题


There are some reference about File Uploading to Azure BY ASP.NET MVC but I can't find none in field of ASP.NET Webpages

How to implement such code to upload to Azure Storage?

Well.. For more information,

My goal is image uploading in CK Editor

but Due to Azure Hosting, ordinary CKEditor reference is not working.

So I googled and use this code block

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

But it doesn't work,

and my 'web.config' is

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

Is anyone ever did upload to Azure Storage via ASP.NET WebPages?

P.S> To be more clear, My 'upload.aspx' source file is this

upload.aspx


回答1:


Sure, you have to upload the file first to you Asp.Net webpage. From there you can upload it to your blobstorage. In your code it seems you are trying to upload a file from the server to blobstorage. First you have to upload the file to server, and then you can send the stream to the blobstorage.




回答2:


I have solved myself! By using Visual Studio, I found a bugging point. olleh!!

Even though I don't like Visual Studio, Visual Studio is very powerful tool Tongue Out

Maybe It's heavyness is worth for that.

This code will work!

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

 .....


来源:https://stackoverflow.com/questions/35749649/file-uploading-to-azure-storage-by-asp-net-webpages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!