Share via

How to upload an Image using FTP in asp.net c#

Ashok Kumar 221 Reputation points
2025-05-06T10:13:43.32+00:00

I'm using the below code to upload an image via FTP concept and at the line of code


using (Stream requestStream = request.GetRequestStream())

            {

                requestStream.Write(fileData, 0, fileData.Length);

            }

I'm getting the following error

System.Net.WebException: 'The remote server returned an error: (550) File unavailable (e.g., file not found, no access).'

But I have full access to use this FTP and what I givent the credentials are corrent and also I have conncet through the "FileZilla" and I able to connected to transfer the image also but in the logic implementation I'm getting the above mentioned error I'm unable to understand the error why it is coming

And new to this FTP concept and suggest me how to solve this error


protected void btnUpload_Click(object sender, EventArgs e)

{

    string uidvalue = Convert.ToString(HttpContext.Current.Session["uidvalue"]);

    if (uidvalue == null)

        uidvalue = "000";

    if (fileUploadImage.HasFile)

    {

        string fileName = Path.GetFileName(fileUploadImage.FileName);

        string fileExtension = Path.GetExtension(fileName).ToLower();

        int fileSize = fileUploadImage.PostedFile.ContentLength;

        // Validate file type

        if (fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".png")

        {

            lblMessage.ForeColor = System.Drawing.Color.Red;

            lblMessage.Text = "Only .jpg, .jpeg and .png files are allowed.";

            return;

        }

        // Validate file size (<= 2MB)

        if (fileSize > 2 * 1024 * 1024)

        {

            lblMessage.Text = "File size must be 2MB or less.";

            return;

        }

        try

        {

            byte[] fileData = fileUploadImage.FileBytes;

            string ftpUrl = "ftp://abc.com/images_c/" + fileName;

            string ftpUsername = "imguser";

            string ftpPassword = "imguserQ123";

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);

            request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);

            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.ContentLength = fileData.Length;

            request.UseBinary = true;

            request.UsePassive = false;

            request.EnableSsl = false;

            request.KeepAlive = false;

            // getting the error in this line 

            using (Stream requestStream = request.GetRequestStream())

            {

                requestStream.Write(fileData, 0, fileData.Length);

            }

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

            {

                lblMessage.ForeColor = System.Drawing.Color.Green;

                lblMessage.Text = "Upload successful: " + response.StatusDescription;

            }

            //lblMessage.ForeColor = System.Drawing.Color.Green;

            //lblMessage.Text = "Upload successful: " + response.StatusDescription;

        }

        catch (Exception ex)

        {

            lblMessage.Text = "Error: " + ex.Message;

        }

    }

    else

    {

        lblMessage.Text = "Please select a file.";

    }

}


Would you like suggestions on how to debug or fix the (550) File unavailable FTP error?

Developer technologies | ASP.NET Core | Other

1 answer

Sort by: Most helpful
  1. returnStructToPFUNC 0 Reputation points
    2025-10-30T03:14:05.2666667+00:00

    Ok well, I don't know C# very well just yet, very object oriented and yeah, I believe C# is one of those heavy callback programming languages

    But I can get you on your somewhat start to VC++ by explaining C++ pointers.

    You see, pointers "point"

    you see, int x = 20; does not in theory point, it allocates memory directly to the variable x

    Where

    int y = 100;

    int *x = &y;

    *x = 200;

    will change both values of the pointer x and the regular int value of y to values both of 200; so you can do y = 150; and *x will "point" to that value

    this is a different scenario:

    int x = new int;

    this is direct memory allocation, and instead of pointing to another variable, allocates a pointer as a normal integer value

    so your code would be as simple as, and I can explain string and characters later, check out chatgpt

    #include <ftp.h>

    #include "main.h"

    int main(){

    string filename = "worker.exe";

    string url = "ftp.company.net";

    followed by structures and other cool classes to place data in functions, I don't know, best I can do to get someone started in c++!

    the rest is easy, install chatgpt on your phone and keep tapping!!

    I have to go install windows on my hp envy, sorry I couldn't help more!!

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.