Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I want to upload some images in the ftp folder.

I performed it and it worked properly in localhost... but now I uploaded the program on the web and it did not work!!! It has this error:

Could not find a part of the path 'C:\Upload\imagename.jpg'

Would you have any idea about this?

- - - - - - - - - - - - - - - - - - - - - - - - -
ASP.NET
<form id="form1" runat="server">

<asp:FileUpload ID="imgFileUpload" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="FTP Upload" />
<br />
<asp:Image id="Image1" runat="server" AlternateText="Image text" ImageAlign="left"/>
</form>

- - - - - - - - - - - - - - - - - - - - - - - - -
C#
private bool UploadToFTP(string strFTPFilePath, string strLocalFilePath, string strUserName, string strPassword)
{
try
{
//Create a FTP Request Object and Specfiy a Complete Path
FtpWebRequest reqObj = (FtpWebRequest)WebRequest.Create(strFTPFilePath);

//Call A FileUpload Method of FTP Request Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile;

//If you want to access Resourse Protected,give UserName and PWD
reqObj.Credentials = new NetworkCredential(strUserName, strPassword);

// Copy the contents of the file to the byte array.
byte[] fileContents = File.ReadAllBytes(strLocalFilePath);
reqObj.ContentLength = fileContents.Length;

//Upload File to FTPServer
Stream requestStream = reqObj.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse();
response.Close();
}

catch (Exception Ex)
{
throw Ex;
}
return true;
}

- - - - - - - - - - - - - - - - - - - - - - - - -

protected void Button1_Click(object sender, EventArgs e)

{
if (imgFileUpload.HasFile)
{
string FileName=imgFileUpload.PostedFile.FileName;

imgFileUpload.SaveAs("C:\\Upload\\" + FileName);
string filePath = "ftp://www.ariavid.com/Upload/";
filePath += FileName;
string fileLocation = "C:\\Upload\\" + FileName;
UploadToFTP(filePath, fileLocation, "username", "password");

Image1.ImageUrl = "ftp://username:password@www.ariavid.com/Upload/" + FileName;
}
}

- - - - - - - - - - - - - - - - - - - - - - - - -

Thanks in advance...

Best Regards,

Shery
Posted
Updated 12-Sep-14 8:37am
v2

The problem has nothing to do with FTP. The path is not found, that's all.

First of all, there are no cases when a hard-coded path name can be useful. The file paths should always be calculated during runtime based on user input, environment and/or some configuration files. In your case, it's not clear why would you save the file locally at all. If this is the ASP.NET application and the file was already uploaded to your site, you already has its content from HTTP request.

Also, you should understand that your ASP.NET page/application cannot access any arbitrary file in the file system of the HTTP server's host computer. The Web applications are normally executed in a sandboxed environment where the access to the host resources is limited, for important security reasons. In particular, you can only access the file system objects under the root directory configured for your site. See also:
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
sheryi26 13-Sep-14 0:24am    
Thanks for your answer.
I will try your solution.
Sergey Alexandrovich Kryukov 13-Sep-14 9:43am    
Great. If you do, consider accepting the answer formally (green "Accept" button). In all cases, your follow-up questions will be welcome.
—SA
Hi,
I changed it to this code, but still it did not work and has an error:

Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\System.Web.HttpInputStream'.

my code here:

C#
private bool UploadToFTP(string strFTPFilePath, string strLocalFilePath, string strUserName, string strPassword)
        {
            try
            {
                //Create a FTP Request Object and Specfiy a Complete Path
                System.Net.FtpWebRequest reqObj = (FtpWebRequest) WebRequest.Create(strFTPFilePath);

                //Call A FileUpload Method of FTP Request Object
                reqObj.Method = WebRequestMethods.Ftp.UploadFile;

                //If you want to access Resourse Protected,give UserName and PWD
                reqObj.Credentials = new NetworkCredential(strUserName, strPassword);

                // Copy the contents of the file to the byte array.
                FileStream fStream = File.OpenRead(strLocalFilePath);
                byte[] fileContents = new byte[fStream.Length];
                fStream.Read(fileContents, 0, (int) fStream.Length);
                reqObj.ContentLength = fileContents.Length;

                //Upload File to FTPServer
                Stream requestStream = reqObj.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse) reqObj.GetResponse();
                response.Close();
            }

            catch (Exception Ex)
            {
                //throw Ex;
                Label1.Text = Ex.ToString();
            }
            return true;
    }

        
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (imgFileUpload.HasFile)
            {
                string FileName = imgFileUpload.PostedFile.FileName;
                string filePath = "ftp://www.ariavid.com/Upload/";
                filePath += FileName;
                UploadToFTP(filePath, imgFileUpload.PostedFile.InputStream.ToString(), "username", "password");

                Image1.ImageUrl = "ftp://username:password@www.ariavid.com/Upload/" + FileName;
            }
        }

Best Regards,
shery
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900