Upload file to WCF Web Api using Hammock

Topics: Web Api
Nov 3, 2011 at 10:43 AM

Hello,

I have the following web api method:

    [WebInvoke(UriTemplate = "save-blob?fileName={fileName}&container={name}&mime={mime}&zineId={id}&notes={notes}&private={isPrivate}")]
        public HttpResponseMessage SavePhotoArticle1(long id, string name, string mime, bool isPrivate,
            string notes, string fileName, HttpRequestMessage request)
        {
            HttpResponseMessage message = null;
            bool isOK = false;
            try
            {
                var client = BlobClientHelper.GetBlobClient();
                var container = client.GetContainerReference(name);
                if (container != null)
                {
                    container.CreateIfNotExist();
                }
 
                var blob = container.GetBlobReference(fileName);
                blob.Metadata["FileType"] = mime;
                blob.Properties.ContentType = mime;
 
                Stream fileContents = request.Content.ContentReadStream;
                
 
                byte[] data = new byte[(int)fileContents.Length];
                
                fileContents.Read(data, 0, data.Length);
                blob.UploadByteArray(data);
                message = new HttpResponseMessage(HttpStatusCode.OK);
                message.Content = new StringContent("Saved" + name);
            }

As you can see, I am passing a bunch of parameters and then saving the uploaded file onto a BLOB on Windows Azure. I'm trying to save this file from my WP7 using a Hammock client with code as follows:

      var client = new RestClient { Authority = App.ZineServiceAuthority, Method = WebMethod.Post };
                        client.AddHeader("Content-Type""multipart/form-data");
                        client.AddHeader("Content-Disposition""form-data");
                        var request = new RestRequest { Path = "articles/save-blob" };
                        request.AddJsonAccept();
                        request.AddToken();
                        request.AddFile("data""test.jpg", stream);
 
                        request.AddParameter("container", userId);
                        request.AddParameter("mime", mimeType);
                        request.AddParameter("zineId", ZineId.ToString());
                        request.AddParameter("notes", txtStatus.Text);
                        request.AddParameter("private""false");
                        request.AddParameter("fileName""test.jpg");

The file gets uploaded but I cannot open the file because it says the 'file has errors'. My guess is that the file is corrupt for some reason. I am also suspecting that the line 'request.AddFile' might have something to do with it. Is there a specific name of the parameter that I need to assign?

Does anyone have a sample of making a simple Http Client call from WP7? Any help would be appreciated.