Win Phone 8 / Asp .Net Web API Image Upload

2019-09-02 07:19发布

问题:

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.

回答1:

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