使用WebClient上传文件时的错误问题解决
2006-03-27 16:23
573 查看
今天在项目中使用WebClient从应用程序上传文件,应该说这是一个很简单的应用,也就调用一个UploadFile方法而已,然而在实验时却遇到了好几个错误,为此郁闷了一个上午,现在把我尝试的经过记录下来,希望对遇到这类问题的朋友有所帮助!开始我是这样写上传代码的:
public class WebClientTest
public class WebClientTest
public class WebClientTest
{
public static void Main(string[] args)
{
// Server URL
string uriString = "http://localhost/FileUpLoad/2006327143303_Grid1.jpg";
// Local Directory File Info
string fileName = @"c:\temp\2006327143303_Grid1.jpg";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] postArray = br.ReadBytes(Convert.ToInt32(fs.Length));
Stream postStream = myWebClient.OpenWrite(uriString,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
}
postStream.Close();
fs.Close();
}
}
这样终于可以了,上传后的图片也能打开了。可是为什么用UploadFile方法上传后的图片打不开呢?
public class WebClientTest
public class WebClientTest
public class WebClientTest
{
public static void Main(string[] args)
{
// Server URL
string uriString = "http://localhost/FileUpLoad/2006327143303_Grid1.jpg";
// Local Directory File Info
string fileName = @"c:\temp\2006327143303_Grid1.jpg";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] postArray = br.ReadBytes(Convert.ToInt32(fs.Length));
Stream postStream = myWebClient.OpenWrite(uriString,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
}
postStream.Close();
fs.Close();
}
}
这样终于可以了,上传后的图片也能打开了。可是为什么用UploadFile方法上传后的图片打不开呢?
相关文章推荐
- 使用WebClient上传文件时的错误问题解决和上传例子
- 使用WebClient上传文件时的错误问题解决
- 使用WebClient上传文件时的错误问题解决
- 使用WebClient上传文件时的错误问题解决
- 使用WebClient上传文件时的错误问题解决
- 解决使用Spring Boot、Multipartfile上传文件路径错误问题
- 解决使用Spring Boot、Multipartfile上传文件路径错误问题
- file文件添加change事件,结合ajaxFileUpload使用文件上传只生效一次问题解决
- 使用autoit解决webdriver文件上传问题
- PHP使用之上传文件到本地服务器(实现上传文件后页面内容不动、解决上传文件命名乱码后缀问题)
- tp3.2整合uploadify上传实现,解决上传中文TP文件上传保存错误问题
- 使用jspsmartupload解决enctype引起的无法同时上传文件和接值的问题
- 如何解决Mysql "发生系统错误2,找不到指定的文件" 的问题(第一次安装使用)
- 解决SWFUpload上传文件组件使用时报告2049错误
- 解决文件无法上传的问题,错误信息:System.IO.DirectoryNotFoundException: Could not find a part of the path.
- 使用ajaxfileupload.js进行文件上传,出现的问题以及解决办法
- 解决asp.net中使用FckEditor上传文件,中文名称乱码的问题
- 使用 Web Service 上传文件至 SPS Document Library 的问题及解决: SPWeb.AllowUnsafeUpdates = true
- 在tp3.2.3中使用 FormData 对象实现表单的文件上传功能遇到的一个问题及解决方法
- 解决MVC下使用uploadify上传文件时HttpPostedFileBase为空的问题