图像不会在网络服务器上传,但在本地就可以

时间:2012-05-04 06:30:08

标签: c# asp.net

我在服务器上有一些图像我希望将其上传到另一台服务器上我制作代码以上传服务器上的所有图像但是可以在本地上传但我不知道有什么问题因为它不能在服务器上传

      try
        {
            byte[] content;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = request.GetResponse();

            Stream stream = response.GetResponseStream();

            using (BinaryReader br = new BinaryReader(stream))
            {
                content = br.ReadBytes(500000);
                br.Close();
            }
            response.Close();

            string CompleteDPath = "ftp path";
            string UName = "abc";
            string PWD = "123";



            WebRequest reqObj = WebRequest.Create(CompleteDPath + file_name);
            reqObj.Method = WebRequestMethods.Ftp.UploadFile;
            reqObj.Credentials = new NetworkCredential(UName, PWD);
            reqObj.GetRequestStream().Write(content, 0, content.Length);
            reqObj = null;



            //FileStream fs = new FileStream(file_name, FileMode.Create);
            //BinaryWriter bw = new BinaryWriter(fs);

            //bw.Write(content);
            //fs.Close();
            //bw.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

1 个答案:

答案 0 :(得分:0)

试一试;

byte[] content;       

HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url);    

WebResponse response1 = request1.GetResponse();

Stream stream = response1.GetResponseStream();

using (BinaryReader br = new BinaryReader(stream))
{
    content = br.ReadBytes((int)stream.Length);
    br.Close();
}
response1.Close();


FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp_path");
request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = new NetworkCredential("abc", "123");
request.ContentLength = content.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(content, 0, content.Length);
requestStream.Close();

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();