我试图将图像上传到imagehost http://uploads.im/.
根据其非常短的API http://uploads.im/apidocs,这是一种方法:
http://uploads.im/api?upload=http://www.google.com/images/srpr/nav_logo66.png
请注意,在此示例中,他正在从互联网上卸载图像,我试图从我的计算机上传文件.
码:
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;
}
}
请看!谢谢!
编辑,新代码:
string filepath = @"c:\Users\xxxx\Desktop\Bilder\images\blank.gif";
using (WebClient client = new WebClient())
{
client.UploadFile("http://uploads.im/api?upload", filepath);
}
我收到错误::基础连接关闭
用try catch编辑:
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);
}
}
最佳答案
private void UploadImage(string filepath)
{
using(WebClient uploader = new WebClient())
{
try
{
uploader.UploadFile(new Uri("http://uploads.im/api?upload"), filepath);
}
catch(Exception ex)
{
MessageBox.Show("An error occured :(\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
我使用http://uploads.im/api?upload作为URL,因为它可以在文档中读取,它将被视为REQUEST,它将自动确定您正在上传图像.
但是我自己尝试了这个代码并且每次都从服务器断开连接而没有任何响应,只是远程主机意外关闭了连接.根据文档,当出现问题时,您甚至应该得到错误的请求响应.我联系了支持人员,希望他们可以告诉我更多相关信息.一旦他们回复我,我会立即更新这个答案.