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!

2
  • 1
    Check Convert object to JSON in C# Commented Jul 3, 2017 at 11:05
  • Have made sure attribute routing is enabled? I would also suggest using a model (DTO) for managing the data Commented Jul 3, 2017 at 11:06

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Ramy, it Works!
@RamyMohamed Hi, What is MemoryPostedFile in second line of the function. I couldn't find anything in MSDN..
@MilindThakkar Check the answer again, I Updated it.
@RamyMohamed: Thanks, I will give it a go and update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.