简体   繁体   中英

Using C# Upload function in VB.Net

This is very specific. I need help with using a C# function in my my VB.Net program The function is in a DLL and the code is below:

public void UploadData(string FTPUri, string FilePath, string FileName,
                       string UserName, string Password)
{
    StatusUp = new Int64[2];

    reqFTP = (FtpWebRequest)FtpWebRequest.Create(FTPUri + FileName);
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential(UserName, Password);

    FileInfo fileInf = new FileInfo(FilePath);

    FileStream fs = fileInf.OpenRead();

    // modifyied code
    int bytesSize = 0;
    byte[] UpBuffer = new byte[2048];

    ftpStream = reqFTP.GetRequestStream();

    bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length);

    while ((bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)) > 0)
    {
        StatusUp[0] = StatusUp[0] + UpBuffer.Length;
        StatusUp[1] = fileInf.Length;// +startPointInt;

        ftpStream.Write(UpBuffer, 0, bytesSize);
    }

    fs.Close();
    ftpStream.Close();
}

In my vb.net program I am calling it like this:

Dim FtpUpload As FTPUtility.ftpUtility = New FTPUtility.ftpUtility
FtpUpload.UploadData("ftp://ftp.xxx.xx", "C:\winzip.log", "/winzip.log", "uploader", "xxxx")

It works ok except it is 2 bytes short when it is done. I don't know enough C# to figure out if the C# code is wrong (I didn't write it and the guy who did has left the company), but somewhere it appears that it is not closing the file or something.

Any ideas?

bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length);

while ((bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)) > 0)
{
    StatusUp[0] = StatusUp[0] + UpBuffer.Length;
    // etc..
}

Two red flags here. The first one is the cause of your problem, the extra fs.Read() call before you enter the while loop. That's 2048 bytes you don't use and don't upload. Just delete that line. You avoid these kind of bugs by using for (;;) and break.

The StatusUp[0] assignment looks bad, you should add bytesSize, not UpBuffer.Length. It isn't otherwise obvious what side-effects that has.

It C#, not C++.

converted using http://www.developerfusion.com/tools/convert/csharp-to-vb/

Public Sub UploadData(FTPUri As String, FilePath As String, FileName As String, UserName As String, Password As String)
    'FtpWebRequest reqFTP; 
    StatusUp = New Int64(1) {}

    reqFTP = DirectCast(FtpWebRequest.Create(FTPUri & FileName), FtpWebRequest)
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile
    reqFTP.UseBinary = True
    reqFTP.Credentials = New NetworkCredential(UserName, Password)
    'StreamReader ReadStream = new StreamReader(FilePath); 
    Dim fileInf As New FileInfo(FilePath)

    Dim fs As FileStream = fileInf.OpenRead()

    ' modifyied code 
    Dim bytesSize As Integer = 0
    Dim UpBuffer As Byte() = New Byte(2047) {}

    'reqFTP.ContentLength = ftpResponse.Length; 
    'Stream ftpStream = reqFTP.GetRequestStream(); 
    ftpStream = reqFTP.GetRequestStream()

    bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)

    While (InlineAssignHelper(bytesSize, fs.Read(UpBuffer, 0, UpBuffer.Length))) > 0
        'StatusUp[0] = ftpStream.Length; 
        StatusUp(0) = StatusUp(0) + UpBuffer.Length
        StatusUp(1) = fileInf.Length
        ' +startPointInt; 
        ftpStream.Write(UpBuffer, 0, bytesSize)
    End While
    fs.Close()
    ftpStream.Close()
    'response.Close(); 
End Sub
Public Sub UploadData(FTPUri As String, FilePath As String, FileName As String, UserName As String, Password As String)
StatusUp = New Int64(1) {}

reqFTP = DirectCast(FtpWebRequest.Create(FTPUri & FileName), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.UploadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(UserName, Password)

Dim fileInf As New FileInfo(FilePath)

Dim fs As FileStream = fileInf.OpenRead()

' modifyied code
Dim bytesSize As Integer = 0
Dim UpBuffer As Byte() = New Byte(2047) {}

ftpStream = reqFTP.GetRequestStream()

bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)

While (InlineAssignHelper(bytesSize, fs.Read(UpBuffer, 0, UpBuffer.Length))) > 0
    StatusUp(0) = StatusUp(0) + UpBuffer.Length
    StatusUp(1) = fileInf.Length
    ' +startPointInt;
    ftpStream.Write(UpBuffer, 0, bytesSize)
End While

fs.Close()
ftpStream.Close()
End Sub

voteup or accept if it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM