【问题标题】:WCF REST Upload Azure BlobWCF REST 上传 Azure Blob
【发布时间】:2016-02-12 12:01:17
【问题描述】:

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

这是我得到的错误:

【问题讨论】:

    标签: c# wcf rest azure azure-blob-storage


    【解决方案1】:

    您正在混合使用经典的 asp.net 和休息服务。在您的代码中,您不使用 asp.net 表单,然后在服务器端代码中调用休息服务。您需要在您的 asp.net 代码中处理该帖子,或者通过在此处发布文件来调用您的休息服务。您作为休息服务入口点的文件名肯定不起作用,因为您通过引用路径从本地服务驱动器获取文件:调用方的 System.IO.File.OpenRead(path)。仅当您在本地(本地主机)进行呼叫时,这才有效。

    【讨论】:

      猜你喜欢
      • 2012-02-18
      • 2013-09-21
      • 2016-10-07
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      相关资源
      最近更新 更多