c# 關於 upload code 無法上傳問題?

請教....
以下的 upload code ,當upload 之後會把原檔檔長變0,upload 上去的檔案也是0,不知是那裡的問題?
可否請高人指點...
tks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using System.Collections;


static class Constants
{
public const String FTP_IP = "ftp://192.168.1.1/";

}



namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{


ftp ftpClient = new ftp(@Constants.FTP_IP, "xxxxx", "xxxxxxxx");


/* Upload a File */
ftpClient.upload("sda/vba20150424175858.rar", @"c:\intel\vba20150424175858.rar");
}
}
}


class ftp
{
private string host = null;
private string user = null;
private string pass = null;
private FtpWebRequest ftpRequest = null;
private FtpWebResponse ftpResponse = null;
private Stream ftpStream = null;
private int bufferSize = 2048;

// Construct Object
public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }


/* Upload File */
public void upload(string remoteFile, string localFile)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
/* Establish Return Communication with the FTP Server */
ftpStream = ftpRequest.GetRequestStream();
/* Open a File Stream to Read the File for Upload */
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
/* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
2015-04-25 0:00 發佈
chengmou

我很佩服你每天一問 半路出家學寫程式當攻城獅的毅力

先抄一個可以用的 https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx

cs113ta wrote:
chengmou我...(恕刪)


謝謝 cs113ta 大大指教...

大大提的我用過了,雖然可以upload,但檔案會變大? 連MSDN 提供的都有問題.....

因為windows 的排程並不符我需求,所以才想寫一支這樣符合自己需求程式
不過我也不是做pc相關工作,也不是要做專業的progmmer
上次寫應該是四、五年前時,下次寫的時候也不知是n年後了.....
也想過要找一個progmmer 付費來幫忙寫,可是這真的只是一個很簡短的程式,才想自己摸看看...

tks.
因為你打錯code了喔!
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
這邊因該是讀取檔案才對,但是你用了FileMode.Create,所以檔案就被覆蓋0byte了!之後上傳也變成0byte很正常。

所以只要把這行改成FileMode.Open讀取檔案就會正常了。
FileStream localFileStream = new FileStream(localFile, FileMode.Open);

cscworm wrote:
因為你打錯code...(恕刪)


csworm 大大....
太感謝了....


tks..

內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?