简体   繁体   中英

c# Send image from WPF to WebAPI

I have a WebAPI 2.1 service (ASP.Net MVC 4) that receive and image and related data. I need to send this image from WPF application, but I get 404 not found error.

Server side

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

Client side

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

The url exists, I thing I need to encode to json format, but I don't know how.

Thanks for your time!

The method parameters in your case are received in QueryString form.

I would suggest you turn the parameters list into one single object like this:

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

Then in you API convert the string to buffer from Base64String like this:

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

Then cast it to HttpPostedFileBase

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

Now you have the image file. Do whatever you want.

Full Code here:

    [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";
    }

Edit: I forgot to add the Code for MemoryPostedFile class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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