using System;
using System.Net;
using System.IO;
using System.Xml;
using System.Web;namespace com.gmcc.itc
{
/// <summary>
/// FileUpload 的摘要说明。
/// </summary>
public class FileUploader
{
private String username;
private String password;
private String url;
private CredentialCache myCredentialCache; public String ErrorMessage; public FileUploader(HttpServerUtility Server)
{
//
// TODO: 在此处添加构造函数逻辑
//
ErrorMessage = ""; //从FileServer.xml中读取配置信息
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("FileSever.xml"));
username = xmlDoc.SelectSingleNode("/fileServer/username").InnerText;
password = xmlDoc.SelectSingleNode("/fileServer/password").InnerText;
url = xmlDoc.SelectSingleNode("/fileServer/url").InnerText; if (url.Length > 0)
{
if (url.Substring(url.Length - 1, 1) == "/")
{
this.url = url;
}
else
{
this.url = url + "/";
}
} //文件服务器认证
myCredentialCache = new CredentialCache();
myCredentialCache.Add(new Uri(this.url), "NTLM", new NetworkCredential(this.username, this.password));
}
public bool UploadFile(Stream inputStream, String filename)
{
ErrorMessage = ""; WebClient fileServer = new WebClient();
fileServer.Credentials = myCredentialCache;
//获取上传文件流
int fileSize = (int)inputStream.Length;
byte[] input = new Byte[fileSize];
inputStream.Read(input, 0, fileSize);

//向文件服务器写入上传文件
Stream writeStream = fileServer.OpenWrite(url + filename, "PUT");
writeStream.Write(input, 0, fileSize); try
{
writeStream.Close();
}
catch (WebException ex)
{
ErrorMessage = ex.Message;
return false;
} return true;
}
        }
}

解决方案 »

  1.   

    前台:
    foreach(string f in Request.Files.AllKeys)
    {
    HttpPostedFile file = Request.Files[f]; if (file.ContentLength > 0) //有文件上传
    {
    if (fu.UploadFile(file.InputStream, System.IO.Path.GetFileName(file.FileName)))
    {
    filename = System.IO.Path.GetFileName(file.FileName);
    Response.Write(System.IO.Path.GetFileName(file.FileName) + ":upload ok<br>");
    }
    else
    {
    Response.Write(System.IO.Path.GetFileName(file.FileName) + ":upload fail:" + fu.ErrorMessage + "<br>");
    return;
    }
    }
    }
      

  2.   

    小弟在岗网上找资料找到有下面那名话"Remoting 关闭了,打开即可"
    请教怎么打开
      

  3.   

    就像你写一个程序用WebClient从别人的网站上取图片保存到本地,如果那个网站关闭了或是该网站删除这图片。那么你同样会出现这样的错误。我想你的错误可能就是这样。
      

  4.   

    Remoting 关闭了,打开即可
    那我想应该是指你服务器里的服务吧。。你看看服务里面是否有Remoting这个。。我也不知道。只是猜的。嘿嘿。
      

  5.   

    我把里面全部remot开头的服务全开启了还是不行
      

  6.   

    是不是使用代理服务器的网络?
    之前用WebRequest的时候就遇到了这样的问题,虽然你的浏览器设置了代理服务器,但它是一个独立于浏览器的对象,所以导致了在使用代理服务器的网络里无法连接远程服务器。
    一直不知道如果是代理服务器的网络该如何使用WebRequest,望高手指点!
      

  7.   

    也出现过。是我在公司调程序需要调用远程的xml文件通过web方式
    回家了上不了网了就出这提示了是远程主机掉线了吧。仅供参考