简体   繁体   English

WCF REST上传Azure Blob

[英]WCF REST Upload Azure Blob

I have created a WCF REst web service with C# where i'm trying to upload a blob in the local Azure Storage Account. 我已使用C#创建了WCF REst Web服务,其中我试图在本地Azure存储帐户中上传Blob。 I created a FileUpload control and a button so that i can choose the file to be uploaded, but i am unable to upload.. Here's my code: 我创建了一个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);
        }

    }

Then in my web form, i have the following design: 然后在我的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> 

And finally here's the code for when i click my button: 最后这是我单击按钮时的代码:

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

This is the error i get: 这是我得到的错误:

错误信息

You are mixing classic asp.net and a rest service. 您正在混合经典的asp.net和Rest服务。 In you code you do a not to a asp.net form and then call in the serverside code a rest service. 在您的代码中,不要对asp.net表单执行操作,然后在服务器端代码中调用rest服务。 You need to handle the post in your asp.net code, or do a call to you rest service by posting the file there. 您需要处理asp.net代码中的帖子,或者通过在其中发布文件来致电您的休息服务。 The filename you have as entry point to you rest service will definitely not work because you are getting the file from you local service drive by referencing the path: System.IO.File.OpenRead(path) of the calling party. 您拥有的作为休息服务入口点的文件名绝对不会起作用,因为您是通过引用以下路径从本地服务驱动器中获取文件的:System.IO.File.OpenRead(path)。 This will only work if you do a call locally (localhost). 仅当您在本地(本地主机)进行呼叫时,此方法才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM