c# – 使用webrequest上传文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用webrequest上传文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1975字,纯文字阅读大概需要3分钟。
内容图文
![c# – 使用webrequest上传文件](/upload/InfoBanner/zyjiaocheng/779/77fe734631ba4c7ca2ba55e0b1a57afd.jpg)
我试图将图像上传到ima??gehost 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,它将自动确定您正在上传图像.
但是我自己尝试了这个代码并且每次都从服务器断开连接而没有任何响应,只是远程主机意外关闭了连接.根据文档,当出现问题时,您甚至应该得到错误的请求响应.我联系了支持人员,希望他们可以告诉我更多相关信息.一旦他们回复我,我会立即更新这个答案.
内容总结
以上是互联网集市为您收集整理的c# – 使用webrequest上传文件全部内容,希望文章能够帮你解决c# – 使用webrequest上传文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。