I have a problem. I want to upload an image to a webpage, so I can store it on my server. Now I can find a lot of examples of uploading files to a server, but what about an Image?
I already tried to create a byte array of the image and send that as a POST, but that crashes on the following line saying that the URI is too long:
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("id", App.User.Id.ToString()));
postData.Add(new KeyValuePair<string, string>("image", ByteArray));
var content = new FormUrlEncodedContent(postData);
Is there another way to do this?
You can try the following method:
//In Xamarin
public void UploadImage()
{
//create an instance of webclient
System.Net.WebClient Client = new System.Net.WebClient();
//add the header
Client.Headers.Add("Content-Type", "binary/octet-streOpenWriteam");
//uploads the file and gets the result in a byte[]
// change `weburl/controller/actionmethod` to our weburl
byte[] result = Client.UploadFile("weburl/controller/actionmethod", "POST", "pathToFile");
//converts the result[] to a string
string resultString = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
}
For more details, you can check: https://forums.xamarin.com/discussion/30653/how-to-upload-image-from-local-storage-to-asp-net-website