locked
Error: Ftp Send File (Pc Client) To The server Ftp host RRS feed

  • Question

  • User2093830819 posted

    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);
    //FileUpload1.SaveAs(serverpath);
    byte[] fileBytes = new byte [1024];

    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());

    fileStream.Close();
    }
    try
    {
    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[1024];
    double total = (double)fileBytes.Length;
    int byteRead = 0;
    double read = 0;
    using (Stream ftpstream = request.GetRequestStream())
    {

    do
    {
    byteRead = fs.Read(fileBytes, 0, 1024);
    ftpstream.Write(fileBytes, 0, byteRead);
    read += byteRead;


    }
    while (byteRead != 0);
    {

    ftpstream.Flush();
    ftpstream.Close();


    }



    }


    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    lblMessage.Text += fileName + "Uploaded.<br />";
    response.Close();
    }
    catch (WebException ex)
    {
    throw new Exception((ex.Response as FtpWebResponse).StatusDescription);

    }
    }


    ftpusername = string.Empty;
    ftppassword = string.Empty;

    }

    Friday, February 21, 2020 7:22 AM

All replies

  • User753101303 posted

    Hi,

    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.

    Friday, February 21, 2020 8:32 AM
  • User-1716253493 posted

    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.

    Saturday, February 22, 2020 2:01 AM
  • User2093830819 posted

    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

    Sunday, February 23, 2020 4:59 AM