Sto creando un semplice drag-file-e-upload-automaticamente-to-FTP applicazione delle finestrefile Caricamento di FTP sono danneggiati una volta nelle strette
e sto usando il MSDN code a carica il file sull'FTP.
Il codice è piuttosto semplice:
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(String.Format("{0}{1}", FTP_PATH, filenameToUpload));
request.Method = WebRequestMethods.Ftp.UploadFile;
// Options
request.UseBinary = true;
request.UsePassive = false;
// FTP Credentials
request.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(fileToUpload.FullName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
writeOutput("Upload File Complete!");
writeOutput("Status: " + response.StatusDescription);
response.Close();
e lo fa ottenere caricati sul FTP
Il problema è quando vedo il file su un browser, o semplicemente scarica e prova a vederlo sul desktop:
Ho già utilizzato request.UseBinary = false;
e request.UsePassive = false;
ma non crea alcun tipo di buono.
Quello che ho scoperto è che, il file originale ha 122Kb lunghezza e nel FTP (e dopo il download), ha 219KB ...
Che cosa sto facendo di sbagliato?
A proposito, il metodo uploadFileToFTP()
è in esecuzione all'interno di un BackgroundWorker
, ma io in realtà non cosa che fa la differenza ...
dati binari e UTF-8 non mescolare bene. – dtb
@dtb usando 'Encoding.ASCII.GetBytes (sourceStream.ReadToEnd());' ottiene lo stesso comportamento ... – balexandre
Sigh. ** I dati binari ** e ** caratteri ** sono due cose diverse. Sì, puoi codificare i caratteri in byte, ma non puoi decodificare ciecamente i byte che non codificano i caratteri. – dtb