将文件上载两次到FTP

时间:2015-06-19 15:49:06

标签: c# .net ftp

我需要将同一个文件(通过WebForm附加方式)上传到两个不同目录中的FTP服务器。

问题是第一次上传没问题,但第二次上传不正确 - 文件丢失或者如果有的话有0长度(是空的)..

这是我的代码(我的FtpManager类):

public void UploadFile(HttpPostedFileBase fileToUpload, string ftpDirPath)
{
    try
    {
        var uploadUrl = string.Format("ftp://{0}//{1}", serverIp, ftpDirPath);
        var uploadFilename = fileToUpload.FileName;
        Stream streamObj = fileToUpload.InputStream;
        byte[] buffer = new byte[fileToUpload.ContentLength];
        streamObj.Read(buffer, 0, buffer.Length);
        streamObj.Close();
        streamObj = null;
        string ftpurl = String.Format("{0}/{1}", uploadUrl, uploadFilename);
        ftpRequest = FtpWebRequest.Create(ftpurl) as FtpWebRequest;
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpRequest.Timeout = 1000000;
        ftpRequest.UseBinary = true;
        ftpRequest.UsePassive = true;
        ftpRequest.KeepAlive = true;
        ftpRequest.Credentials = new NetworkCredential(username, password);
        Stream requestStream = ftpRequest.GetRequestStream();
        requestStream.Write(buffer, 0, buffer.Length);
        requestStream.Flush();
        requestStream.Close();
        requestStream = null;

        FtpWebResponse uploadResponse = (FtpWebResponse)ftpRequest.GetResponse();
        uploadResponse.Close();

        ftpRequest = null;

    }
    catch
    {
        throw;
    }
}

我这样用:

string serverIp = SERVER;
string user = FTP_USER;
string pass = FTP_PASSWORD;
string ftpDir1 = "var/www/rrhh/_lib/tmp";
string ftpDir2 = "var/www/rrhh/docs";

var ftpManager = new FtpManager(serverIp, user, pass);

ftpManager.UploadFile(file, ftpDir1);
ftpManager.UploadFile(file, ftpDir2);

所以我的问题是我的第二次我的方法有效(不会抛出异常),但是没有(正确上传文件)?

PS。

分析结果:

FtpWebResponse uploadResponse = (FtpWebResponse)ftpRequest.GetResponse();
response = "Status Code: {0}; Description: {1}".Fill(
    uploadResponse.StatusCode,
    uploadResponse.StatusDescription);
uploadResponse.Close();

首次上传

Status Code: ClosingData; Description: 226 File receive OK.

第二次上传:

Status Code: ClosingData; Description: 226 File receive OK.

3 个答案:

答案 0 :(得分:1)

InputStream读取后,您将结束,第二次获得空字节数组。在致电streamObj.Position = 0之前使用streamObj.Read()返回InputStream的开头。

答案 1 :(得分:0)

我建议将文件保存到临时目录,例如。使用:

var fileName = System.IO.Path.GetTempFileName();
file.SaveAs(fileName);

[...]
ftpManager.UploadFile(fileName, ftpDir1);
ftpManager.UploadFile(fileName, ftpDir2);
System.IO.File.Delete(fileName);

然后更改您的UploadFile方法以改为使用文件名:

public void UploadFile(string fileToUpload, string ftpDirPath)
[...]
Stream streamObj = File.OpenRead(fileToUpload);
byte[] buffer = new byte[streamObj.Length];
[...]

答案 2 :(得分:0)

像这样修改(添加了bool closeStream):

public void UploadFile(HttpPostedFileBase fileToUpload, 
                       string ftpDirPath, bool closeStream)
{
    try
    {
        var uploadFilename = fileToUpload.FileName;
        Stream streamObj = fileToUpload.InputStream;
        byte[] buffer = new byte[fileToUpload.ContentLength];
        streamObj.Position = 0;
        streamObj.Read(buffer, 0, buffer.Length);
        if (closeStream)
        {
            streamObj.Close();
            streamObj = null;
        }
        ...

用法:

ftpManager.UploadFile(file, ftpDir1, false);
ftpManager.UploadFile(file, ftpDir2, true);