繁体   English   中英

使用对象上传Web API 2图像

[英]web API 2 image upload with Object

我想上传具有某些属性值的图像。 我只能上传图片,这是我的图片上传代码:

    [HttpPost]
    [Route("UploadImage")]
    public async Task<HttpResponseMessage> ImageUpload()
    {
        // Check whether the POST operation is MultiPart?
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        // Prepare CustomMultipartFormDataStreamProvider in which our multipart form
        // data will be loaded.
        string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data");
        CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);
        List<string> files = new List<string>();

        try
        {
            // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
            await Request.Content.ReadAsMultipartAsync(provider);

            foreach (MultipartFileData file in provider.FileData)
            {
                files.Add(Path.GetFileName(file.LocalFileName));
            }

            // Send OK Response along with saved file names to the client.
            return Request.CreateResponse(HttpStatusCode.OK, files);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

CustomMultipartFormDataStreamProvider类:

// We implement MultipartFormDataStreamProvider to override the filename of File which
// will be stored on server, or else the default name will be of the format like Body-
// Part_{GUID}. In the following implementation we simply get the FileName from 
// ContentDisposition Header of the Request Body.
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
    public CustomMultipartFormDataStreamProvider(string path) : base(path) { }

    public override string GetLocalFileName(HttpContentHeaders headers)
    {
        return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
    }

}

邮递员测试的功能 在此处输入图片说明

我想像这样传递到目前为止无法实现的数据

{
    "name": "Mr.AAAA",
    "fatherName":"Mr.BBBB",
    "userProfileImage" : "image goes here"?
}

如何实现呢?

我可能会考虑这样做略有不同,以实现您想要的。 在上传之前先研究一下base64编码的文件,然后将其作为另一文本块来处理JSON格式变得更加容易

看看这篇文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM