Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Below is my C# code to upload images in to the FTP. My problems is:

1. I want to able to get full path in filename variable to get correct file or any other option to solve it.

2. How to avoid following error while uploading: Request for the permission of type
'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. 


Dear Experts, please give me simple advise to upload a images to ftp, because I'm searching this around 2-3 days and I'm really disappointed. Thanks!
C#
protected void UploadButton_Click(object sender, EventArgs e)
{
 if (FileUploadControl.HasFile)
            {
                try
                {
                    if (FileUploadControl.PostedFile.ContentType == "image/jpeg")
                    {
                        if (FileUploadControl.PostedFile.ContentLength < 5242881)
                        {
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                                                    
                            Stream image;
                                string target = "1.jpg";

                                FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://ftppath" + target);
                                req.UseBinary = true;
                                req.Method = WebRequestMethods.Ftp.UploadFile;
                                req.Credentials = new NetworkCredential("ftpUname", "ftpPass");

                                byte[] fileData = File.ReadAllBytes(filename);

                                req.ContentLength = fileData.Length;
                                Stream reqStream = req.GetRequestStream();
                                reqStream.Write(fileData, 0, fileData.Length);
                                reqStream.Close();                            
                             
                             StatusLabel.Text = "Upload status: File uploaded!";
                        }
                        else
                            StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                    }
                    else
                        StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
}
Posted
v4

1 solution

Full path of the file can be access by using below function
C#
string filepath = Path.getfilename (fileupload1.filename);

rgarding permission issue :
you need to set permission for everyone group on that folder with modify right. don't give full access but modify is needed to resolve the permission issue.
 
Share this answer
 
v2
Comments
abdulsafran 20-Dec-13 8:26am    
Hi ravikhoda, Thanks for your response. Regarding to get the full path I have used same, but I can't get full path because in some web browsers not allow to get full path since security concerns. Therefore, I need a solution to get file name full path using file uploader or any other methods (Cannot use openfiledialog in asp.net).

Second, I have provided all rights to the particular folder and I gave user name password but still that public token is occur.

Please help me.
raneshtiwari 20-Dec-13 9:07am    
You want full path right?
then user Server.MapPath("filename");
you have to know where your directory is located then you can do the such type thing, see below code may be help full to you ..
this.Server.MapPath("~/" + System.IO.Path.GetFileName(fileName))
abdulsafran 20-Dec-13 11:22am    
raneshtiwari Thanks for your response, when I use this code this.Server.MapPath("~/" + System.IO.Path.GetFileName(fileName)) it's get programm located path + filename. This is not I want. I want is user upload their image path, that path I want, it will be anywhere in the hard drive. Please help me.
abdulsafran 20-Dec-13 11:31am    
or Please help me how to upload image on ftp without any problem?

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