Uploading an IMG file through RestSharp

802 views Asked by At

I have the code:

    request.AddFile ("image_request[image]", ("picture.jpg");

The "image_request[image]" is required for the specific API. The "picture.jpg" is in my /resources/ folder and correctly works.

However, with my application, I do not have a file stored locally. I take a picture with the camera app and have it stored as a photo called img, which I can convert to a .jpg

My question is: How can I upload the file when I do not have a path? I simply have the .jpg image stored in a variable. RestSharp asks for a string path as the second parameter in the method AddFile, however, as stated before, I do not have a path.

I want to be able to do something like

    request.AddFile ("image_request[image]", (img);
1

There are 1 answers

1
Nick Klufas On BEST ANSWER

Path.GetTempFileName

Path.GetTempPath

It sounds like you may want to write the file to a temporary file, this would allow you to be in compliance with .AddFile's method signature

EDIT

//  Get an Image instance
Image image;
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
    image = Image.FromStream(fs);
    //  Save to a temp file - this is the code that throws the exception
    image.Save(Path.GetTempFileName());
}