I am trying to adjust the upload.ashx in such a way that it stores the files to an ftp location. I am getting it to work if the user selects a single file, however if the user selects multiple files it only up loads the last file.
the code below is called in the process request function
HttpPostedFile postedFile = context.Request.Files["Filedata"];
BinaryReader b = new BinaryReader(postedFile.InputStream);
byte[] buffer = b.ReadBytes(postedFile.ContentLength);
if (ftconn.upload(value, buffer) == true)
{
b.Close();
context.Response.StatusCode = 200;
}
else {
context.Response.StatusCode = 500;
}
the ftp upload function
public bool upload(string remoteFile, byte[] localFilebytes)
{
bool statval = true;
try
{
/* Create an FTP Request
*/
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(@_url.Trim() + remoteFile.Trim());
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(_username, _password);
ftpRequest.UseBinary = true;
ftpRequest.Proxy = null;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
/* Establish Return Communication with the FTP Server */
Stream ftpStream = ftpRequest.GetRequestStream();
/* Open a File Stream to Read the File for Upload */
// FileStream localFileStream = new FileStream(localFile.Trim(), FileMode.OpenOrCreate);
/* Buffer for the Downloaded Data */
// byte[] byteBuffer = localFilebytes.Length;
// byte[] byteBuffer = File.ReadAllBytes(localFile);
int bytesSent = localFilebytes.Length;
/* 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);
// //bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
//}
ftpStream.Write(localFilebytes, 0, bytesSent);
}
catch (Exception ex)
{
// Console.WriteLine(ex.ToString());
statval = false;
}
/* Resource Cleanup */
//localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex)
{
// Helper.aid = ex.ToString();
//Console.WriteLine(ex.ToString());
statval = false;
}
return statval;
}