Im пытается загрузить изображение в изображениеhost http://uploads.im/
.Загрузить файл с webrequest
Согласно его очень короткому API http://uploads.im/apidocs
это способ сделать это:
http://uploads.im/api?upload=http://www.google.com/images/srpr/nav_logo66.png
Обратите внимание, что в этом примере он upploading изображения из Интернета и Im пытается загрузить файл с компьютера.
Код:
public ActionResult SaveUploadedFile()
{
//Converts the image i want to upload to a bytearray
Image postData = img;
byte[] byteArray = imageToByteArray(postData);
//Is this adress not correct maybe? Is there a way to test?
WebRequest wrq = WebRequest.Create("http://uploads.im/api?upload=");
wrq.Method = ("POST");
//Im thinking that here I need som code
//that specifys the file i want to upload (bytearray)
using (WebResponse wrs = wrq.GetResponse())
using (Stream stream = wrs.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
string json = reader.ReadToEnd();
tempJson = json;
}
}
Пожалуйста, смотрите! Благодаря!
EDIT, новый код:
string filepath = @"c:\Users\xxxx\Desktop\Bilder\images\blank.gif";
using (WebClient client = new WebClient())
{
client.UploadFile("http://uploads.im/api?upload", filepath);
}
Я получаю ошибку:: Базовое соединение закрыт
EDIT с попыткой поймать:
string filepath = @"c:\Users\xxxx\Desktop\sack.png";
using (WebClient client = new WebClient())
{
try
{
client.UploadFile("http://uploads.im/api?upload", filepath);
}
catch (Exception e)
{
throw new ApplicationException(e);
}
}
Пожалуйста, добавьте сообщение об исключении (e.Message). – kobigurk
Исключение типа «System.Net.WebException» произошло в System.dll, но не было обработано в коде пользователя Дополнительная информация: Основное соединение было закрыто: соединение было неожиданно закрыто. – user2915962
Что я имел в виду, это обернуть бит 'client.UploadFile' с помощью try/catch и показать там сообщение. – kobigurk