赢手机8 / ASP的.NET Web API上传图片 [英] Win Phone 8 / Asp .Net Web API Image Upload

查看:369
本文介绍了赢手机8 / ASP的.NET Web API上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code负责上传图片:

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 });
    });
}

它通过jQuery POST请求的作品。但是,在Win 8手机应用程序,以下code似乎没有做出对API的请求:

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;
    });
}

这里有什么问题吗?我希望有人能告诉我正确使用的HttpClient。

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

推荐答案

这是一个典型的土耳其İ问题。改变图像/上传到图像/上传解决了这个问题。

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

这篇关于赢手机8 / ASP的.NET Web API上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆