Last post Feb 23, 2020 04:59 AM by fouzan
Feb 21, 2020 07:22 AM|fouzan|LINK
i Spend more time For search the solution of transfer File to the server host via asp webform put the problem is the :: FileStream fs = File.OpenRead(fileName); ::
fileName Send Me to The Current folder Location not The file selected I want upload it him means if i put the copy location and past him is work examble : C:\\.....image.png .
put filename not recognizer .
see the code :
//FTP Server URL.
string ftpFolder = @"Views/";
String ftp = "ftp://myftp/"; // e.g. fake IDs
String ftpusername = "usr"; // e.g. fake username
String ftppassword = "pass"; // e.g. fake password
// string serverpath = Server.MapPath(FileUpload1.FileName);
byte fileBytes = new byte ;
foreach (HttpPostedFile fileupload in FileUpload1.PostedFiles)
string serverpath = Server.MapPath(fileupload.FileName);
string fileName = Path.GetFileName(fileupload.FileName);
using (StreamReader fileStream = new StreamReader(fileupload.InputStream))
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
// request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ftpusername, ftppassword);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
request.Timeout = -1;
// FileStream fs = File.OpenRead(@"\\Mac\Home\Desktop\"+fileName);
// this is true path \\Mac\Home\Desktop\.........png
// check if i put tru pthe here is work file
FileInfo fInfo = new FileInfo(serverpath);
FileStream fs = File.OpenRead(fileName); // here we must put path of the exist file or don't work
fileBytes = new byte;
double total = (double)fileBytes.Length;
int byteRead = 0;
double read = 0;
using (Stream ftpstream = request.GetRequestStream())
byteRead = fs.Read(fileBytes, 0, 1024);
ftpstream.Write(fileBytes, 0, byteRead);
read += byteRead;
while (byteRead != 0);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
lblMessage.Text += fileName + "Uploaded.<br />";
catch (WebException ex)
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
ftpusername = string.Empty;
ftppassword = string.Empty;
Feb 21, 2020 08:32 AM|PatriceSc|LINK
Keep in mind that when working on a dev machine you are in the very special case where the same machine is both the web server and the browsser side machine.
You are using the file upload input tag and usually the actual client side location is not posted to the server for safety reason. Anyway if running on a real server you would not have access anyway to the client side drive.
As for a usual download upload you'll lhave to use the FileContent stream to read the uploaded file on your web server and then write this content to the final FTP server.
Feb 22, 2020 02:01 AM|oned_gk|LINK
Don't think about client path. Maybe security reason not allow you to see their resources or some information.
You only need to save file upload to your resources.
Feb 23, 2020 04:59 AM|fouzan|LINK
thanks i found the error
the problem is run server upload ftp inside client server and i put the file direct in server and upload from client pc .
thanks for all