Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi ,

the below code works well on local server and i'm able to upload file..

HttpFileCollection uploadFilCol = Request.Files;
                try
                {
                    for (int i = 0; i < uploadFilCol.Count; i++) //uploadFilCol.Count
                    {
                        HttpPostedFile file = uploadFilCol[i];
                        string fileExt = Path.GetExtension(file.FileName).ToLower();
                        string fileName = Path.GetFileName(file.FileName);
                      
                        if (file.ContentLength > 0)
                        {

                            if (fileName != string.Empty)
                            {
                               
                                createdir(ftpaddress + Session["userid"].ToString(), user, pass);
                                try
                                {
                                   
                                    if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png" || fileExt == ".jpeg" || fileExt == ".rar" || fileExt == ".zip" || fileExt == ".7z")
                                    {
                                       
                                        FtpWebRequest uploadRequest = (FtpWebRequest)FtpWebRequest.Create(ftpaddress + Session["userid"].ToString() + @"/" + fileName);
                                        Stream requestStream = null;
                                        FtpWebResponse uploadResponse = null;
                                        try
                                        {
                                            Thread.Sleep(20);
                                            uploadRequest.Credentials = new NetworkCredential(user, pass);
                                            uploadRequest.KeepAlive = false;
                                            uploadRequest.UsePassive = false;
                                            uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
                                            uploadRequest.UseBinary = true;
                                            uploadRequest.Timeout = 10000000;
                                            byte[] buffer = null;
                                            uploadRequest.Proxy = null;
                                            requestStream = uploadRequest.GetRequestStream();
                                            long bytesToRead = file.ContentLength;
                                            int bytesRead = 0;

                                            while (bytesRead < bytesToRead)
                                            {
                                                buffer = new byte[file.ContentLength];

                                                bytesRead += file.InputStream.Read(buffer, 0, buffer.Length);
                                                if (bytesRead == 0)
                                                    break;

                                                requestStream.Write(buffer, 0, bytesRead);
                                              
                                            }


                                            requestStream.Close();
                                            uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
                                           
                                        }
                                        catch (UriFormatException ex)
                                        {
                                            lbl_error.Text = ex.Message;
                                        }
                                        catch (IOException ex)
                                        {
                                            lbl_error.Text = ex.Message;
                                        }
                                        catch (WebException ex)
                                        {
                                            lbl_error.Text = ex.Message;
                                        }
                                        finally
                                        {
                                            if (uploadResponse != null)
                                                uploadResponse.Close();
                                         
                                            if (requestStream != null)
                                                requestStream.Close();
                                        }
      
                                        this.ShowMessage("" + fileName + " Uploaded ", i);

                                    }
                                    else
                                    {
                                        this.ShowMessage(" <font color=red> This is not a Picture File</font/>", i);
                                    }
                                    //}
                                }
                                catch (Exception ex)
                                {
                                    this.ShowMessage("<font color=red> " + fileName + " Not Uploaded <font>", i);
                                }
                            }
                           
                        }
                        else
                        {
                            this.ShowMessage("<font color=red> " + fileName + " File size not more than 6MB</font>", i);
                        }
                      
                    }
                   

                }
               
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("default.aspx");
        }


    }


But it is not working after the site is hosted on the server.. that is, the file is not uploading.. also it doesn't returns any error.. Am i missing something?.. Pls help me out...

Thanks...
Posted
Updated 14-Oct-10 7:21am
v2

HI try to do the followings

1. Check the ftp host address,and config file for updating server details
2. Alos check the credentials , if no permission then also file will not get uploaded to server

MIDL
ftp.Credentials = new NetworkCredential("userid", "password");
    //userid and password for the ftp server to given


3. While creating FtpWebRequest.Create why you are passing Session ID please check, and try to do as below

C++
string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
 
Share this answer
 
v2
I think the problem of permission. check the permission settings for asp.net and network security. if you not set permission file will not save in server.
 
Share this answer
 
Comments
arun.emm 15-Oct-10 1:58am    
Hi ,

Rajesh n raju... there is no problem with host address or username or password.. this coding works well if i run it on local server. even i'll be able to upload the larger file.. but after hosting , its no longer working. after hosted , it works for smaller files, say below 2 mb but not working for the larger files.. any suggestions??..
raju melveetilpurayil 15-Oct-10 5:26am    
normally asp.net support uploading file upto 4MB. if you need to upload bigger files you need to add some setting in web.confing file.
if you need to upload more than 4MB you can this code in wen.config

VB
<httpRuntime
 maxRequestLength="4096"


or check this link
http://msdn.microsoft.com/en-us/library/aa479405.aspx[^]
 
Share this answer
 
v2
Comments
raju melveetilpurayil 15-Oct-10 5:39am    
maxRequestLength="4096"

this is for 4MB. You can change this value

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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