0

I am working with mvc 4.5 trying to upload an image using FTP.

My code:

public virtual void Send(HttpPostedFileBase uploadfile, string fileName)
{
    Stream streamObj = uploadfile.InputStream;
    byte[] buffer = new byte[uploadfile.ContentLength];
    streamObj.Read(buffer, 0, buffer.Length);
    streamObj.Close();

    string ftpurl = String.Format("{0}/{1}/{2}", _remoteHost, _foldersHost, fileName);
    var request = FtpWebRequest.Create(ftpurl) as FtpWebRequest;

    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UsePassive = false;
    request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
    request.ContentLength = buffer.Length;

    var requestStream = request.GetRequestStream();
    requestStream.Write(buffer, 0, buffer.Length);
    requestStream.Close();

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

    if (response != null)
        response.Close();
}

The code is working on localhost, I am able to conect to the FTP server and upload the file, but the problem comes once the project is published. I am using SmarterASP to host the site. When I try to upload the image on production, the uploading method gives me an exception, the message says "Unable to connect to the remote server".

Any clue or workaround?

Exception details:

System.Net.FtpWebRequest.GetRequestStream() at AgileResto.Controllers.RestaurantsController.Send(HttpPosted‌​FileBase uploadfile, String fileName) at AgileResto.Controllers.RestaurantsController.UploadFile(Int3‌​2 RestaurantList, HttpPostedFileBase file, HttpPostedFileBase file2)

  • Have you checked firewall ports, and that you can actually connect to the FTP site from the production server? As a basic test, try browsing to ftp:// in your browser of choice to see if the connection is successful. If not, you have connection/configuration issues to worry about before you fix any code. – pcdev May 22 '17 at 02:08
  • Do you have any reason to the active mode (`UsePassive = false`)? It can hardly work. Use the passive mode. – Martin Prikryl May 22 '17 at 05:19
  • If you need more help, start by posting exact exception message, full stack trace and [`FtpWebRequest` log file](http://stackoverflow.com/q/9664650/850848). – Martin Prikryl May 22 '17 at 05:20
  • @pcdev I am using SmarterASP to host the site, I am afraid I can't access to firewall config.. From my pc I can't reach anything but an error page when I try browsing ftp://. But it is weird that I am able to reach FTP server from my site running at localhost, isn't it? – Agustin Villar May 22 '17 at 15:01
  • @MartinPrikryl, I changed it to passive mode but the error persists.. .I try to create log file, but i can't find it. Do you know where I can find it? The full exception: _Unable to connect to the remote server at System.Net.FtpWebRequest.GetRequestStream() at AgileResto.Controllers.RestaurantsController.Send(HttpPostedFileBase uploadfile, String fileName) at AgileResto.Controllers.RestaurantsController.UploadFile(Int32 RestaurantList, HttpPostedFileBase file, HttpPostedFileBase file2)_ – Agustin Villar May 22 '17 at 15:02
  • 1) *"From my pc I can't reach anything"* and *"I am able to reach FTP server from my site running at localhost"* seem contradictory to me. 2) I've asked you for a log file. 3) Edit the exception details to the question, do not post it in comments. 4) Do you have any reason to believe you have a network connectivity from the SmarterASP to the FTP server? Can you test it anyhow? – Martin Prikryl May 22 '17 at 15:10
  • @MartinPrikryl, I mean that the test mentioned by _pcdev_ doesn't seems to be working for me, but when I run my site from VS, the site can connect to FTP server and upload the file. 2) I cant find the log fle, do you know the location? 4) I guess that there is network connectivity between site and FTP as FTP is given by smarterASP also. I have wrote to smarterASP support team to ask about a possible FTP network issue related to this, but have no answer yet. Do you think it could be related more to a FTP conection issue than code issue? – Agustin Villar May 22 '17 at 15:41
  • Either the FTP server is not reachable from the web server at all. Or the local (as of the web server) account does not have a network privileges. Neither is actually a programming question. – Martin Prikryl May 22 '17 at 17:10

1 Answers1

0

you need to grant write permission in the server to the folder you are saving the file to

enter image description here

thanzeel
  • 431
  • 4
  • 12