Загрузка веб-API 2 с помощью Object - PullRequest
0 голосов
/ 25 февраля 2019

Я хочу загрузить изображение с некоторыми значениями свойств.Я могу только загрузить изображение Вот мой код загрузки изображения:

    [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 class:

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

}

Функция проверена почтальоном enter image description here

Я хочу передать данные, которых я не могу достичь до сих пор, например

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

Как этого добиться?

1 Ответ

0 голосов
/ 25 февраля 2019

Возможно, я бы подумал сделать это немного иначе, чтобы достичь того, чего вы хотите.Посмотрите в base64 кодировку ваших файлов перед загрузкой, тогда вам будет намного легче работать в формате JSON, просто как очередной блок текста

Взгляните на эту статью

...