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

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;
                }
}

Always getting following error while I have given full right to the particular folder.

'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Please help me.
Posted
Updated 15-Jan-14 21:43pm
v2
Comments
Sibasisjena 16-Jan-14 3:34am    
Where do you want to save the image in database or in folder.
abdulsafran 16-Jan-14 3:40am    
I want to save it's inside the folder "ftp path + images/profile/"
JoCodes 16-Jan-14 3:54am    
In web.config are you having <trust level="Full" />?
abdulsafran 16-Jan-14 4:53am    
If I put trust level as Full, occur below error:

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

Source Error:

Line 15: <!--<sessionstate cookieless="true"
line="" 16:="" regenerateexpiredsessionid="true"> -->
Line 17: <trust level="Full" />
Line 18:
Line 19: <appsettings>

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055
JoCodes 17-Jan-14 23:52pm    
Try this link in case you are having error when using full trust.
http://geekswithblogs.net/ranganh/archive/2005/04/25/37609.aspx

1 solution

 
Share this answer
 
v2
Comments
abdulsafran 16-Jan-14 7:41am    
I just need to upload images in asp.net, please advise how to do it perfectly. Thanks!
JoCodes 17-Jan-14 0:09am    
Its more related to the trust level if you are Webpermission since FTP is used.
abdulsafran 17-Jan-14 9:36am    
Everything is working perfectly when I'm using this on my hard desk, once uploaded to web site. This problem occurring. Please let me know how to correct it?

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