ftp fileupload错误 [英] ftp fileupload error

查看:80
本文介绍了ftp fileupload错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我使用下面的函数上传文件从我的远程位置..

但我得到错误:"

System.Net.WebException:数据连接是由一个不同于FTP连接地址的地址建立的。


在System.Net.FtpWebRequest.SyncRequestCallback(Object obj)


at System.Net.FtpWebRequest.ReallCallback(Object obj)


at System.Net.CommandStream.Abort(Exception e)


在System.Net.FtpWebRequest.FinishRequestStage(RequestStage阶段)


在System.Net.FtpWebRequest.GetRequestStream()




"
所以请参阅功能和帮助我公共

"
所以请查看功能并帮助我 public





bool 上传( FileInfo fi, 字符串 targetFilename)

{


<字体大小= 2>



//将指定的文件复制到目标文件:目标文件可以是完整路径或j ust文件名(使用当前目录)





// 1。检查目标





字符串 target;



if (targetFilename.Trim()== ""

{





//空白目标:使用源文件名&当前目录



target =



。CurrentDirectory + fi.Name;

}





else if (targetFilename.Contains( " /" ))

{





//如果包含/视为完整路径



目标= AdjustDir(targetFilename);


}





else



{





//否则仅视为文件名,使用当前目录



target = CurrentDirectory + targetFilename;


}





string URI =主机名+目标;



//执行复制



System.Net。



FtpWebRequest ftp = GetRequest(URI);



//设置上传二进制文件的请求



ftp.Method = System.Net。



WebRequestMethods Ftp 。UploadFile; < font size = 2>

ftp.UseBinary =



true ;

ftp.UsePassive =



false ;



//通知预期大小的FTP



ftp.ContentLength = fi.Length;


< font size = 2>



//创建要存储的字节数组:确保至少1个字节!





const int BufferSize = 2048;



byte [] content = byte [BufferSize - 1 + 1] ;



int dataRead;





// WebResponse response = ftp。 GetResponse()。status;







//打开文件进行阅读





使用 FileStream fs = fi.OpenRead())

{





尝试



{





//打开发送请求





Stream rs = ftp.GetRequestStream();







{


dataRead = fs.Read(content ,0,BufferSize);


rs.Write(content,0,dataRead);



}



while (!(dataRead< BufferSize));

rs.Close();




}





catch WebException ex)

{





< span style ="font-size:x-small; color:#008000"> // String status =((FtpWebResponse)e.Response).StatusDescription;





// FtpWebResponse response =(FtpWebResponse)ftp.GetResponse( );





字符串 错误;

error = ex.ToString();





return false ;

}





最后



{





//确保文件关闭



fs.Close();


}


}




ftp =



null ;



return true ;

}


解决方案

我打赌这有什么关系处于Active或Passive连接以及后面的安全性。这有帮助吗?
http:// social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/4f634a97-bef2-4273-a49c-a6f71126772e


 

I m Using The Below Function To Upload File From My Remote Location..

But i m Getting Error :"

System.Net.WebException: The data connection was made from an address that is different than the address to which the FTP connection was made.

at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)

at System.Net.FtpWebRequest.RequestCallback(Object obj)

at System.Net.CommandStream.Abort(Exception e)

at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)

at System.Net.FtpWebRequest.GetRequestStream()

 

"
So Please See The Function and Help me
public

"
So Please See The Function and Help me
public

 

bool Upload(FileInfo fi, string targetFilename)

{

 

//copy the file specified to target file: target file can be full path or just filename (uses current dir)

 

//1. check target

 

string target;

 

if (targetFilename.Trim() == "")

{

 

//Blank target: use source filename & current dir

target =

this.CurrentDirectory + fi.Name;

}

 

else if (targetFilename.Contains("/"))

{

 

//If contains / treat as a full path

target = AdjustDir(targetFilename);

}

 

else

{

 

//otherwise treat as filename only, use current directory

target = CurrentDirectory + targetFilename;

}

 

string URI = Hostname + target;

 

//perform copy

System.Net.

FtpWebRequest ftp = GetRequest(URI);

 

//Set request to upload a file in binary

ftp.Method = System.Net.

WebRequestMethods.Ftp.UploadFile;

ftp.UseBinary =

true;

ftp.UsePassive =

false;

 

//Notify FTP of the expected size

ftp.ContentLength = fi.Length;

 

//create byte array to store: ensure at least 1 byte!

 

const int BufferSize = 2048;

 

byte[] content = new byte[BufferSize - 1 + 1];

 

int dataRead;

 

 

//WebResponse response = ftp.GetResponse().status;

 

 

//open file for reading

 

using (FileStream fs = fi.OpenRead())

{

 

try

{

 

//open request to send

 

Stream rs = ftp.GetRequestStream();

 

 

do

{

dataRead = fs.Read(content, 0, BufferSize);

rs.Write(content, 0, dataRead);

}

while (!(dataRead < BufferSize));

rs.Close();

 

 

}

 

catch (WebException ex)

{

 

//String status = ((FtpWebResponse)e.Response).StatusDescription;

 

// FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();

 

string error;

error = ex.ToString();

 

return false;

}

 

finally

{

 

//ensure file closed

fs.Close();

}

}

 

ftp =

null;

 

return true;

}

解决方案

I bet this has something to do with the connection being Active or Passive and the security behind that.  does this help?
http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/4f634a97-bef2-4273-a49c-a6f71126772e


这篇关于ftp fileupload错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆