C#如何将大文件上传到FTP站点

编程入门 行业动态 更新时间:2024-10-23 06:25:05
本文介绍了C#如何将大文件上传到FTP站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个用于备份的Windows应用程序(Files and sql server database)。 现在我需要将这些文件(.rar文件)上传到我的ftp站点。 上传我使用这段代码。

I am developing an windows application for backup(Files and sql server database). Now i need to upload these files(.rar files) to my ftp site. For uploading i use this code.

string file = "D:\\RP-3160-driver.zip"; //opening the file for read. string uploadFileName = "", uploadUrl = ""; uploadFileName = new FileInfo(file).Name; uploadUrl = "ftp://ftp.Sitename/tempFiles/"; FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); try { long FileSize = new FileInfo(file).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("usernam", "password"); Stream requestStream = requestObj.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); requestStream.Flush(); requestStream.Close(); requestObj = null; MessageBox.Show("File upload/transfer Successed.", "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { if (fs != null) { fs.Close(); } MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message, "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information); }

这段代码只上传那些尺寸大于< 5 Mb。 但是我需要将大于500Mb的文件上传到1Gb文件。 所以任何人都可以帮助我。

This code uploads only those files which has size < 5 Mb. But i need to upload larger then 500Mb to 1Gb files. So can any one help me.

推荐答案

对于较大的文件,您可以选择读取文件流并写入在读取它时输出流。

For larger files you may choose to read the file stream and write it to the output stream as you read it.

FileStream fs = null; Stream rs = null; try { string file = "D:\\RP-3160-driver.zip"; string uploadFileName = new FileInfo(file).Name; string uploadUrl = "ftp://ftp.Sitename/tempFiles/"; fs = new FileStream(file, FileMode.Open, FileAccess.Read); string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName); FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest; requestObj.Method = WebRequestMethods.Ftp.UploadFile; requestObj.Credentials = new NetworkCredential("usernam", "password"); rs = requestObj.GetRequestStream(); byte[] buffer = new byte[8092]; int read = 0; while ((read = fs.Read(buffer, 0, buffer.Length)) != 0) { rs.Write(buffer, 0, read); } rs.Flush(); } catch (Exception e) { MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message, "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (fs != null) { fs.Close(); fs.Dispose(); } if (rs != null) { rs.Close(); rs.Dispose(); } }

更多推荐

C#如何将大文件上传到FTP站点

本文发布于:2023-11-10 00:56:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   大文件   站点   FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!