我使用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);
}
}
然后在我的网络表单中,我有以下设计:
<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);
}
}
这是我得到的错误:
答案 0 :(得分:1)
您正在混合经典的asp.net和休息服务。在你的代码中,你不是一个asp.net表单,然后在服务器端代码中调用一个休息服务。您需要在asp.net代码中处理帖子,或者通过在那里发布文件来调用您的休息服务。作为您休息服务的入口点的文件名肯定不会起作用,因为您通过引用主叫方的路径:System.IO.File.OpenRead(path)从本地服务驱动器获取文件。这仅适用于您在本地进行呼叫(localhost)。