日期:2014-05-18  浏览次数:20800 次

图片文件的上传与保存
我想将图片文件从客户端上传到服务器上,我的是winform,
 我将文件从本地上传到服务器时,首先用file.copy的方法,但只能本局域网内部上传,因要进行外部上传所以我改用下面的方法,但还是不能上传,但也没有报错,请大家帮忙找下原因

 private void btn_save_Click(object sender, EventArgs e)
  {
  NetworkCredential credentials =new NetworkCredential("administrator", "ISAweb");
  topf.DemonstrateWebClient(txt_fileroad.Text, credentials);
  }

public void DemonstrateWebClient(string sourcestr, NetworkCredential credentials)
  {  
  WebClient webClient = new WebClient();
  webClient.Credentials = credentials;  

  // Upload file.
  try
  {
  string fileName = sourcestr.Substring(sourcestr.LastIndexOf("\\") + 1, sourcestr.Length - sourcestr.LastIndexOf("\\") - 1); //ReadFileName("upload");
  //string uploadAddress = serverAddress + fileName;
  Uri address1 = new Uri("http://www.toploria.com.cn/TKservice/Pictures/");
  //Console.WriteLine("Uploading file " + uploadAddress);
  webClient.UploadFile(address1, fileName);
  }
  catch (WebException ex)
  {
  Console.WriteLine(ex.Message);
  }  

  webClient.Dispose();
  }

------解决方案--------------------
你应该是没有访问权限,尝试一下开一个ftp将文件copy到ftp上~~~
------解决方案--------------------
以前写的一个东西.或许楼主有借鉴的价值.谢谢
protected void Button1_Click(object sender, EventArgs e)
{
string savePath = "c:\\";
string fileName = FileUpload1.FileName;

if (FileUpload1.HasFile)//FileUpload1是控件ID {
string pathToCheck = savePath + fileName;
if (!System.IO.File.Exists(pathToCheck))
{
FileUpload1.PostedFile.SaveAs(pathToCheck);
}
else
{
int count = 2;
string pathToCheck2 = savePath + count.ToString() + fileName;
FileUpload1.PostedFile.SaveAs(pathToCheck2);
}
}
else
{
this.Button1.Text = "No File";
}
}
希望能对楼主有用,也能够起到抛砖引玉的作用,谢谢.
------解决方案--------------------
看你在哪一步得到错误信息的,打个断点跟1下,还有这个http://www.toploria.com.cn/TKservice/Pictures的虚拟目录权限是否可写,这个站点的WEBDAV服务是不是允许