[英]WCF REST Upload Azure Blob
我已使用C#創建了WCF REst Web服務,其中我試圖在本地Azure存儲帳戶中上傳Blob。 我創建了一個FileUpload控件和一個按鈕,以便可以選擇要上傳的文件,但是無法上傳。這是我的代碼:
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);
}
}
然后在我的Web表單中,我有以下設計:
<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);
}
}
這是我得到的錯誤:
您正在混合經典的asp.net和Rest服務。 在您的代碼中,不要對asp.net表單執行操作,然后在服務器端代碼中調用rest服務。 您需要處理asp.net代碼中的帖子,或者通過在其中發布文件來致電您的休息服務。 您擁有的作為休息服務入口點的文件名絕對不會起作用,因為您是通過引用以下路徑從本地服務驅動器中獲取文件的:System.IO.File.OpenRead(path)。 僅當您在本地(本地主機)進行呼叫時,此方法才有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.