0
我用C#創建了一個WCF REst Web服務,我試圖在本地Azure存儲帳戶中上傳一個Blob。我創建了一個FileUpload控件和一個按鈕,這樣我可以選擇要上傳的文件,但我無法上傳.. 這裏是我的代碼:WCF REST上傳Azure Blob
public class Service : IService
{
[WebInvoke(Method = "POST", UriTemplate = "Upload", ResponseFormat =
WebMessageFormat.Json)]
public void Upload(string path)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnection"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//Get the reference of the container in which the blobs are stored
CloudBlobContainer container = blobClient.GetContainerReference("upload");
//Set the name of the uploaded document to a unique name
string docName = "Document_" + Guid.NewGuid().ToString() + ".txt";
//Get the blob reference and set its metadata properties
CloudBlockBlob blockBlob = container.GetBlockBlobReference(docName);
using (var fileStream = System.IO.File.OpenRead(path))
{
blockBlob.UploadFromStream(fileStream);
}
}
然後在我的網頁形式,我有以下的設計:
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="uplFileUpload" runat="server" />
<br /><br />
</div>
<asp:Button ID="btnUpload" runat="server" Text="Upload"
onclick="btnUpload_Click" />
<asp:Button ID="btnDisplayBlobs" runat="server" Text="Display Blobs"
onclick="btnDisplayBlobs_Click" />
<br />
<p>
<asp:Label ID="lblURIs" runat="server" Text=""></asp:Label>
</p>
</form>
最後在這裏是爲當我點擊我的按鈕的代碼:
public partial class WebClient : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
Service blob = new Service();
if (uplFileUpload.HasFile)
{
blob.Upload(uplFileUpload.PostedFile.FileName);
}
}
這是錯誤我得到:
好的,謝謝!我會嘗試一下 – Huby03