Win Phone 8 / Asp .Net Web API Image Upload

怎甘沉沦 提交于 2019-12-11 22:20:12

问题


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.



来源:https://stackoverflow.com/questions/19192638/win-phone-8-asp-net-web-api-image-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!