在一个会话中将多个文件上载到FTp服务器 [英] Upload Multiple Files to a FTp server within one session

查看:71
本文介绍了在一个会话中将多个文件上载到FTp服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我需要将多个文件上传到ftp服务器,我试图使用Webrequest一次发送每个文件,问题是每次我发送我需要添加凭据的文件,这意味着我打开了一个新会话。我尝试了不同的方法,但它不起作用,低于我最新的。有谁知道如何优雅地做到这一点?我需要一次发送文件,所以我不能使用异步。有人可以帮我吗?



  private   void  envoiFTP( string  table)
{
string path = @ D:\Temp \;
//
string [] files = Directory.GetFiles(path, @ * .xml);

if (files!= null
{
foreach 字符串文件 个文件)
{
fi = new FileInfo(file);
string fileName = fi.Name;
string fileurl = path + @ / + fileName;
string ftpFile = FtpServer + @ / + fileName;
FtpWebRequest myRequest =(FtpWebRequest)FtpWebRequest.Create(ftpFile);

myRequest.Credentials = new NetworkCredential(FtpUser,FtpPassword);

myRequest.Method = WebRequestMethods.Ftp.UploadFile;
myRequest.Timeout = 1000000 ;
myRequest.UseBinary = true ;
myRequest.KeepAlive = true ;

myRequest.ContentLength = fi.Length;

byte [] buffer = new byte [ 4097 ];
int bytes = 0 ;
int total_bytes =( int )fi.Length;
System.IO.FileStream fs = fi.OpenRead();
System.IO.Stream rs = myRequest.GetRequestStream();

while (total_bytes > 0
{
bytes = fs.Read(buffer, 0 ,buffer.Length);
rs.Write(buffer, 0 ,bytes);
total_bytes = total_bytes - bytes;
}

fs.Close();
rs.Close();

FtpWebResponse uploadResponse =(FtpWebResponse)myRequest.GetResponse();
uploadResponse.Close();
}
}
}
}

解决方案





如果您想异步上传文件:链接 [ ^ ]

更多信息 [ ^ ]



[更新]



http:// ftplib。 codeplex.com/ [ ^ ]

嗨试试下面代码在FTP服务器上传文件





private void Upload144P_Click(对象发送者) ,EventArgs e)

{

OpenFileDialog fileobj = new OpenFileDialog();

fileobj.Filter =电影文件(* .mp4)| * .mp4 |网络电影文件(* .webm)| * .webm |所有文件(*。*)| *。*;

//fileobj.InitialDirectory =C:\\\ \\ ;;

//fileobj.Filter =视频文件(* .mp4);

//fileobj.ShowDialog();



if(fileobj.ShowDialog()== DialogResult.OK)

{

if(fileobj.CheckFileExists)

{

string test = Properties.Settings.Default.Connection;

SqlConnection con = new SqlConnection(test);

con。打开();

string corr ectfilename = System.IO.Path.GetFileName(fileobj.FileName);

SqlCommand cmd = new SqlCommand(Insert into Path(ID,Path5)VALUES((select isnull(MAX(id),0 )来自Path的+ 1,'\\Videos \\+ correctfilename +'),con);





cmd.ExecuteNonQuery();

//对于进度条

DataTable dt = new DataTable();



timer5.Enabled = true;



string path = Application.StartupPath.Substring(0,Application.StartupPath.Length - 10);

con.Close();





string uploadfile = fileobj.FileName;

string uploadFileName = new FileInfo(uploadfile).Name;



string uploadUrl = ftp://ftp.infotech.com/;

FileStream fs = new FileStream(uploadfile,FileMode.Open,FileAccess.Read);



try

{

long FileSize = new FileInfo(uploadfile).Length; //正在上传的文件的文件大小。

Byte [] buffer = new Byte [FileSize];



fs.Read(buffer, 0,buffer.Length);



fs.Close();

fs = null;

string ftpUrl = string.Format({0} / {1},uploadUrl,uploadFileName);

FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl)as FtpWebRequest;

requestObj .Method = WebRequestMethods.Ftp.UploadFile;

requestObj.Credentials = new NetworkCredential(test@infotech.com,test @ 123);

Stream requestStream = requestObj.GetRequestStream();

requestStream.Write(buffer,0,buffer.Length);



requestObj.Timeout = 1000000; < br $> b $ b requ estStream.Flush();

requestObj = null;



//requestObj.ConnectionGroupName =MyGroupName;

//requestObj.KeepAlive = false;

//requestObj.ServicePoint.CloseConnectionGroup(\"MyGroupName);



requestObj.UsePassive = true;

requestObj.UseBinary = true;

requestObj.KeepAlive = false;

}

catch(例外情况ex)

{

//MessageBox.Show(文件上传/传输失败。\\\\ nn错误消息:\\\\ n+ ex .Message,成功,MessageBoxButtons.OK,MessageBoxIcon.Information);

}

}

}

}


Hello,

I need to upload multiple files to an ftp server, i tried to send each file a time using Webrequest, the problem is that each time I send a file i need to add credentials , which means i open up a new session. I tried different approach but it didn't work, below my latest. Does anyone have an idea how to do it gracefully? I need to send a file a time, so i can't use asynchronous. can anyone help me please ?

private void envoiFTP(string table)
        {
              string path = @"D:\Temp\";
                //
                string[] files = Directory.GetFiles(path,@"*.xml");        
               
                if (files != null)
                {                    
                    foreach (string file in files)
                    {
                        fi = new FileInfo(file);
                        string fileName = fi.Name;
                        string fileurl = path + @"/" + fileName;
                        string ftpFile = FtpServer + @"/" + fileName;
                        FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);

                        myRequest.Credentials = new NetworkCredential(FtpUser, FtpPassword);

                        myRequest.Method = WebRequestMethods.Ftp.UploadFile;
                        myRequest.Timeout = 1000000;
                        myRequest.UseBinary = true;
                        myRequest.KeepAlive = true;
                     
                        myRequest.ContentLength = fi.Length;

                        byte[] buffer = new byte[4097];
                        int bytes = 0;
                        int total_bytes = (int)fi.Length;
                        System.IO.FileStream fs = fi.OpenRead();
                        System.IO.Stream rs = myRequest.GetRequestStream();

                         while (total_bytes > 0)
                         {
                             bytes = fs.Read(buffer, 0, buffer.Length);
                             rs.Write(buffer, 0, bytes);
                             total_bytes = total_bytes - bytes;
                         }

                         fs.Close();
                         rs.Close();

                         FtpWebResponse uploadResponse = (FtpWebResponse)myRequest.GetResponse();
                         uploadResponse.Close();
                    }                                     
                }                  
         }                           
   }          

解决方案

Hi,

If you want to upload file asynchronously : Link[^]
Some More info[^]

[update]

http://ftplib.codeplex.com/[^]


hi try this below code for upload file in FTP Server


private void Upload144P_Click(object sender, EventArgs e)
{
OpenFileDialog fileobj = new OpenFileDialog();
fileobj.Filter = "Movie files (*.mp4)|*.mp4|Web Movie files (*.webm)|*.webm|All files (*.*)|*.*";
//fileobj.InitialDirectory = "C:\\";
//fileobj.Filter = "Video files (*.mp4)";
//fileobj.ShowDialog();

if (fileobj.ShowDialog() == DialogResult.OK)
{
if (fileobj.CheckFileExists)
{
string test = Properties.Settings.Default.Connection;
SqlConnection con = new SqlConnection(test);
con.Open();
string correctfilename = System.IO.Path.GetFileName(fileobj.FileName);
SqlCommand cmd = new SqlCommand("Insert into Path(ID,Path5) VALUES ((select isnull(MAX(id),0) + 1 from Path),'\\Videos\\" + correctfilename + "')", con);


cmd.ExecuteNonQuery();
//For Progressbar
DataTable dt = new DataTable();

timer5.Enabled = true;

string path = Application.StartupPath.Substring(0, Application.StartupPath.Length - 10);
con.Close();


string uploadfile = fileobj.FileName;
string uploadFileName = new FileInfo(uploadfile).Name;

string uploadUrl = "ftp://ftp.infotech.com/";
FileStream fs = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);

try
{
long FileSize = new FileInfo(uploadfile).Length; // File size of file being uploaded.
Byte[] buffer = new Byte[FileSize];

fs.Read(buffer, 0, buffer.Length);

fs.Close();
fs = null;
string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
requestObj.Credentials = new NetworkCredential("test@infotech.com", "test@123");
Stream requestStream = requestObj.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);

requestObj.Timeout = 1000000;
requestStream.Flush();
requestObj = null;

//requestObj.ConnectionGroupName = "MyGroupName";
//requestObj.KeepAlive = false;
//requestObj.ServicePoint.CloseConnectionGroup("MyGroupName");

requestObj.UsePassive = true;
requestObj.UseBinary = true;
requestObj.KeepAlive = false;
}
catch (Exception ex)
{
//MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message, "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}


这篇关于在一个会话中将多个文件上载到FTp服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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