0
votes

I have created a WCF REst web service with C# where i'm trying to upload a blob in the local Azure Storage Account. 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:

    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:

<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:

Error Message

1

1 Answers

1
votes

You are mixing classic asp.net and a rest service. In you code you do a not to a asp.net form and then call in the serverside code a rest service. You need to handle the post in your asp.net code, or do a call to you rest service by posting the file there. 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. This will only work if you do a call locally (localhost).