Xamarin表单将图像和变量上传到网页

问题描述 投票:0回答:1

我有问题。我想将图像和变量上传到我的网页,所以我创建了以下代码:

using (var formContent = new MultipartFormDataContent("NKdKd9Yk"))
{
    formContent.Headers.ContentType.MediaType = "multipart/form-data";

    StringContent UserIdContent = new StringContent(App.User.Id.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded");
    formContent.Add(UserIdContent, "id");

    StringContent CreatedImageContent = new StringContent(CreatedImage, Encoding.UTF8, "binary/octet-streOpenWriteam");
    formContent.Add(CreatedImageContent , "image");

    using (var client = new HttpClient())
    {
        try
        {
            // 4.. Execute the MultipartPostMethod
            var message = await client.PostAsync(url, formContent);
            // 5.a Receive the response
            var result = await message.Content.ReadAsStringAsync();

            if (result == "Success")
            {
                App.Current.MainPage = new SideMenuItems();
            }
        }
        catch (Exception ex)
        {
            // Do what you want if it fails.
            throw ex;
        }
    }
}

现在两个变量都已被服务器接收,但是图像变量是设备上图像的路径,而不是图像本身。

我在做什么错?

c# xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
0
投票
using (var formContent = new MultipartFormDataContent("NKdKd9Yk")) { formContent.Headers.ContentType.MediaType = "multipart/form-data"; var id = App.User.Id.ToString(); StringContent UserIdContent = new StringContent(id, Encoding.UTF8, "application/x-www-form-urlencoded"); formContent.Add(UserIdContent, "id"); FileStream fs = System.IO.File.OpenRead(CreatedImage); formContent.Add(new StreamContent(fs), "image"); using (var client = new HttpClient()) { try { // 4.. Execute the MultipartPostMethod var message = await client.PostAsync(url, formContent); // 5.a Receive the response var result = await message.Content.ReadAsStringAsync(); if (result == "Success") { App.Current.MainPage = new SideMenuItems(); } } catch (Exception ex) { // Do what you want if it fails. throw ex; } } }
© www.soinside.com 2019 - 2024. All rights reserved.