2

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!

Duefectu
  • 1,563
  • 4
  • 18
  • 37
  • 1
    Check [Convert object to JSON in C#](https://stackoverflow.com/questions/11345382/convert-object-to-json-string-in-c-sharp) – Siva Gopal Jul 03 '17 at 11:05
  • Have made sure attribute routing is enabled? I would also suggest using a model (DTO) for managing the data – Nkosi Jul 03 '17 at 11:06

1 Answers1

5

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; }
}
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45