2016-02-12 108 views
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);    
     } 
    } 

這是錯誤我得到:

Error Message

回答

1

你混合經典asp.net和休息服務。在你的代碼中,你做了一個不是asp.net表單,然後在服務器端代碼中調用一個休息服務。你需要在你的asp.net代碼中處理帖子,或者通過在那裏發佈文件來打電話給你休息服務。作爲入口點休息服務的文件名肯定不起作用,因爲您通過引用主叫方的System.IO.File.OpenRead(路徑)路徑從本地服務驅動器獲取文件。這隻有在本地進行呼叫(本地主機)時纔有效。

+0

好的,謝謝!我會嘗試一下 – Huby03