PHP expert |
|
Ik probeer bestanden te uploaden dmv FTP. Dit lukt wel maar ik heb nu een foto van 3MB die ik wil uploaden, hij upload wel maar krijg daarna een foutmelding (Connection unexpectedly terminated).
De code die ik nu heb zit in een loop en de eerste 13 bestanden upload hij goed maar dit bestand upload hij en geeft dan een foutmelding terug.
Weet iemand hoe ik dit kan oplossen? Of weet iemand een andere manier waarop ik (grote) bestanden kan uploaden?
public void UploadFile(string FTPAddress, string directory, string filename, string username, string password)
{
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(FTPAddress + "/" + directory + "/" + Path.GetFileName(filename));
Request.Method = WebRequestMethods.Ftp.UploadFile;
Request.Credentials = new NetworkCredential(username, password);
Request.UsePassive = true;
Request.UseBinary = true;
Request.KeepAlive = false;
FileStream fs = File.OpenRead(filename);
int dataLength = (int)fs.Length;
int buffLength = 4096;
byte[] buffer = new byte[buffLength];
progressBarUpload.Value = 0;
progressBarUpload.Maximum = dataLength;
lbProgress.Text = "0/" + dataLength.ToString();
try
{
Stream strm = Request.GetRequestStream();
int bytesRead = fs.Read(buffer, 0, buffer.Length);
while (true)
{
if (bytesRead == 0)
{
progressBarUpload.Value = progressBarUpload.Maximum;
lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString();
lbProcentFile.Text = "100%";
Application.DoEvents();
strm.Close();
fs.Close();
break;
}
else
{
strm.Write(buffer, 0, bytesRead);
bytesRead = fs.Read(buffer, 0, buffLength);
if (progressBarUpload.Value + bytesRead <= progressBarUpload.Maximum)
{
progressBarUpload.Value += bytesRead;
lbProgress.Text = progressBarUpload.Value.ToString() + "/" + dataLength.ToString();
int procentStap = (progressBarUpload.Value * 100) / dataLength;
lbProcentFile.Text = procentStap.ToString() + "%";
progressBarUpload.Refresh();
Application.DoEvents();
}
}
}
FtpWebResponse response = (FtpWebResponse)Request.GetResponse();
// MessageBox.Show("Upload File Complete, status " + response.StatusDescription);
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "Upload Error");
}
}
public void UploadFile(string FTPAddress, string directory, string filename, string username, string password) { FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(FTPAddress + "/" + directory + "/" + Path.GetFileName(filename)); Request.Method = WebRequestMethods.Ftp.UploadFile; Request. Credentials = new NetworkCredential (username, password ); Request.UsePassive = true; Request.UseBinary = true; Request.KeepAlive = false; FileStream fs = File.OpenRead(filename); int dataLength = (int)fs.Length; int buffLength = 4096; byte[] buffer = new byte[buffLength ]; progressBarUpload.Value = 0; progressBarUpload.Maximum = dataLength; lbProgress.Text = "0/" + dataLength.ToString(); try { Stream strm = Request.GetRequestStream(); int bytesRead = fs.Read(buffer, 0, buffer.Length); while (true) { if (bytesRead == 0) { progressBarUpload.Value = progressBarUpload.Maximum; lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString(); lbProcentFile.Text = "100%"; Application.DoEvents(); strm.Close(); fs.Close(); break; } else { strm.Write(buffer, 0, bytesRead); bytesRead = fs.Read(buffer, 0, buffLength); if (progressBarUpload.Value + bytesRead <= progressBarUpload.Maximum) { progressBarUpload.Value += bytesRead; lbProgress.Text = progressBarUpload.Value.ToString() + "/" + dataLength.ToString(); int procentStap = (progressBarUpload.Value * 100) / dataLength; lbProcentFile.Text = procentStap.ToString() + "%"; progressBarUpload.Refresh(); Application.DoEvents(); } } } FtpWebResponse response = (FtpWebResponse)Request.GetResponse(); // MessageBox.Show("Upload File Complete, status " + response.StatusDescription); response.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message + "Upload Error"); } }
|