0

The following code responsible for uploading images:

[HttpPost]
public async Task<HttpResponseMessage> Upload()
{
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    var streamProvider = new MultipartMemoryStreamProvider();

    Cloudinary cloudinary = new Cloudinary(ConfigurationManager.AppSettings.Get("CLOUDINARY_URL"));

    return await Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
    {
        if (t.IsFaulted || t.IsCanceled)
            throw new HttpResponseException(HttpStatusCode.InternalServerError);

        var content = streamProvider.Contents.FirstOrDefault().ReadAsStreamAsync();

        ImageUploadParams uploadParams = new ImageUploadParams()
        {
            File = new CloudinaryDotNet.Actions.FileDescription(Guid.NewGuid().ToString(), content.Result)
        };

        ImageUploadResult uploadResult = cloudinary.Upload(uploadParams);

        string url = cloudinary.Api.UrlImgUp.BuildUrl(String.Format("{0}.{1}", uploadResult.PublicId, uploadResult.Format));

        return Request.CreateResponse<MediaModel>(HttpStatusCode.Created, new MediaModel { URL = url });
    });
}

It works via jquery post request. However, in win phone 8 application, the following code does not seem to make a request to the api:

public async Task<string> UploadImage(byte[] image)
{
    var client = new HttpClient();

    var content = new MultipartFormDataContent();

    var imageContent = new ByteArrayContent(image);

    imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");

    content.Add(imageContent, "image", string.Format("{0}.jpg", Guid.NewGuid().ToString()));

    return await client.PostAsync(baseURL + "image/Upload", content).Result.Content.ReadAsStringAsync().ContinueWith(t =>
    {
        return t.Result;
    });
}

What is the problem here? I hope someone could show me the proper use of httpclient.

2
  • what is the error message you receive? Commented Oct 5, 2013 at 21:45
  • It doesn't throw any exception. App just can't make a request to the api but other api methods (such as Get methods) work fine. Commented Oct 5, 2013 at 22:19

1 Answer 1

1

It is a classic Turkish İ problem. Changing "image/Upload" to "Image/Upload" solved the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.