c# 将图像从 WPF 发送到 WebAPI

标签 c# wpf asp.net-web-api

我有一个 WebAPI 2.1 服务 (ASP.Net MVC 4),用于接收图像和相关数据。 我需要从 WPF 应用程序发送此图像,但收到 404 未找到错误。

服务器端

[HttpPost]
[Route("api/StoreImage")]
public string StoreImage(string id, string tr, string image)
{
    // Store image on server...
    return "OK";
}

客户端

public bool SendData(decimal id, int time, byte[] image)
{
    string url = "http://localhost:12345/api/StoreImage";
    var wc = new WebClient();
    wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    var parameters = new NameValueCollection()
    {
        { "id", id.ToString() },
        { "tr", time.ToString() },
        { "image", Convert.ToBase64String(image) }
    };
    var res=wc.UploadValues(url, "POST", parameters);
    return true;
}

该网址存在,我想我需要编码为 json 格式,但我不知道如何。

感谢您的宝贵时间!

最佳答案

您的案例中的方法参数以QueryString 形式接收。

我建议您将参数列表变成一个单独的对象,如下所示:

public class PhotoUploadRequest
{
    public string id;
    public string tr;
    public string image;
}

然后在 API 中将字符串从 Base64String 转换为缓冲区,如下所示:

 var buffer = Convert.FromBase64String(request.image);

然后将其转换为HttpPostedFileBase

  HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(buffer);

现在您已经有了图像文件。做你想做的。

完整代码:

    [HttpPost]
    [Route("api/StoreImage")]
    public string StoreImage(PhotoUploadRequest request)
    {
        var buffer = Convert.FromBase64String(request.image);
        HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(buffer);
        //Do whatever you want with filename and its binaray data.
        try
        {

            if (objFile != null && objFile.ContentLength > 0)
            {
                string path = "Set your desired path and file name";

                objFile.SaveAs(path);

                //Don't Forget to save path to DB
            }

        }
        catch (Exception ex)
        {
           //HANDLE EXCEPTION
        }

        return "OK";
    }

编辑: 我忘记添加 MemoryPostedFile 类的代码

 public class MemoryPostedFile : HttpPostedFileBase
{
    private readonly byte[] fileBytes;

    public MemoryPostedFile(byte[] fileBytes, string fileName = null)
    {
        this.fileBytes = fileBytes;
        this.FileName = fileName;
        this.InputStream = new MemoryStream(fileBytes);
    }
    public override void SaveAs(string filename)
    {
        File.WriteAllBytes(filename, fileBytes);
    }
    public override string ContentType => base.ContentType;

    public override int ContentLength => fileBytes.Length;

    public override string FileName { get; }

    public override Stream InputStream { get; }
}

关于c# 将图像从 WPF 发送到 WebAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44883945/

相关文章:

c# - 创建用户时读取超时错误c#

c# - Thread.Sleep c#.NET

c# - 无法找到合适的构造函数错误

具有多个子级的 WPF UserControl

c# - WPF 应用程序中的组合框,具有全选/取消全选功能

c# - Web Api 方法接受字符串、整数、字母数字

.net - 将代理与 .NET 4.5 HttpClient 一起使用

c# - 列表 <BusinessObject> 还是 BusinessObjectCollection?

c# - 如何处理 ComboBox 的 SelectedIndexChanged 事件?

c# - WPF MVVM 如何使用 ShowDialog() 上的绑定(bind)预填充文本框?