-2

i use this code for ftp upload from this link.

and when i use that, show me that error , what am i doing?

Picture of Error When Used That Code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Net;
using System.Text;

public partial class CS : System.Web.UI.Page
{
    protected void FTPUpload(object sender, EventArgs e)
    {
        //FTP Server URL.
        string ftp = "92.222.117.211";

        //FTP Folder name. Leave blank if you want to upload to root folder.
        string ftpFolder = "Uploads/";

        byte[] fileBytes = null;

        //Read the FileName and convert it to Byte array.
        string fileName = Path.GetFileName(FileUpload1.FileName);
        using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
        {
            fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
            fileStream.Close();
        }

        try
        {
            //Create FTP Request.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            //Enter FTP Server credentials.
            request.Credentials = new NetworkCredential("foxseria", "244RujnnL2");
            request.ContentLength = fileBytes.Length;
            request.UsePassive = true;
            request.UseBinary = true;
            request.ServicePoint.ConnectionLimit = fileBytes.Length;
            request.EnableSsl = false;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileBytes, 0, fileBytes.Length);
                requestStream.Close();
            }

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

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

and this is a my code, and the ftp server and username - password are correct.

lokusking
  • 7,396
  • 13
  • 38
  • 57
Dariush Gavari
  • 27
  • 2
  • 11
  • i want create the page, and my users browse any files , then click on button , then that file upload to server with FTP. – Dariush Gavari Sep 10 '16 at 10:04
  • 1. Show your code. 2. Don't paste screenshots, paste actual code. 3. If you have more information to add, edit the question, rather than comments (people may not read it then). Overall: [How to Ask](http://stackoverflow.com/help/how-to-ask) – AgataB Sep 10 '16 at 10:42
  • 1
    @AgataB I edit my question and show my code – Dariush Gavari Sep 10 '16 at 11:22
  • Additionally, I sincerely hope you didn't post the actual plaintext credentials to your FTP server. If you did, you've had a security hole for quite some time. _NEVER_ post actual credentials on the Internet! – Andrew Gray Aug 11 '20 at 16:05

1 Answers1

2

You are missing the ftp:// prefix, and a slash between the hostname/IP address and the folder name, in your URL.

Your would-be URL is:

92.222.117.211Uploads/filename

While a real URL is like:

ftp://92.222.117.211/Uploads/filename
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • thanks, i found new error while upload. Exception of type 'System.Exception' was thrown. Line 57: catch (WebException ex) Line 58: { Line 59: throw new Exception((ex.Response as FtpWebResponse).StatusDescription); Line 60: } Line 61: } how to fix this? thanks. – Dariush Gavari Sep 12 '16 at 11:50
  • 1
    In your new question, make sure you share some information about the exception. Your comment does not include anything that can be used to debug your problem. – Martin Prikryl Sep 12 '16 at 11:52