File Uploading & DownLoading in Ftp Server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bhargavi venkat
    New Member
    • Apr 2013
    • 1

    File Uploading & DownLoading in Ftp Server

    while Uploading the file its giving the error like this..

    But Local Systems its working fine.
    Plz help me...

    Thanks in Advance...




    [DirectoryNotFou ndException: Could not find a part of the path 'C:\Documents and Settings\User1\ Desktop\Misc\br anchcount.pdf'.]
    System.IO.__Err or.WinIOError(I nt32 errorCode, String maybeFullPath) +193
    System.IO.FileS tream.Init(Stri ng path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIB UTES secAttrs, String msgPath, Boolean bFromProxy) +1162
    System.IO.FileS tream..ctor(Str ing path, FileMode mode, FileAccess access) +67
    System.Net.WebC lient.OpenFileI nternal(Boolean needsHeaderAndB oundary, String fileName, FileStream& fs, Byte[]& buffer, Byte[]& formHeaderBytes , Byte[]& boundaryBytes) +203
    System.Net.WebC lient.UploadFil e(Uri address, String method, String fileName) +253

    [WebException: An exception occurred during a WebClient request.]
    System.Net.WebC lient.UploadFil e(Uri address, String method, String fileName) +538
    ResumedtlsEntry .btnSubmit_Clic k(Object sender, EventArgs e) +5654
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +111
    System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String eventArgument) +110
    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument) +10
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +36
    System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +1565
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't know much about ASP.Net but I know whoever can help you won't be able to without seeing your code.

    Comment

    • cybarcom
      New Member
      • Apr 2013
      • 1

      #3
      Some time we need to download, upload file from FTP server. Here is some example to FTP operation.
      For this we need to include one namespace and it is. using System.Net

      A good reference regarding this http://cybarlab.blogspot.com/2013/03...-from-ftp.html

      Comment

      • bharathreddy
        New Member
        • Aug 2006
        • 116

        #4
        Please find the code below:

        Code:
        public Main()
        {
        
        \\ ctlFileUpload is the System.Web.UI.WebControls.FileUpload control on the form.
        
        Stream uploadStream = ctlFileUpload.PostedFile.InputStream;
        int iTotal = int.Parse(uploadStream.Length.ToString());
        byte[] all = new byte[uploadStream.Length];
        byte[] newbyte;
        uploadStream.Read(all, 0, iTotal);
        memstr.Write(all, 0, iTotal);
        memstr.Seek(0, SeekOrigin.Begin);
        
        
        string strCompleteFTPPath = "\\ftp-P\PathName";
        String strFileName = "MyFileName.txt";
        
        Boolean bFileUpload = FTPFileUpload(memstr, strCompleteFTPPath, strFileName);
        
        if(bFileUpload)
        {
        Response.Write("File Uplaoded!");
        }
        else
        {
        Response.Write("Error while uploading file to ftp server");
        }
        
        }
        
        
        
        public Boolean FTPFileUpload(Stream streamObj, string strCompleteFTPPath, string strFileName)
        {
                    strFTPUserName = "UserName";
                    strFTPPassword = "Password for connecting to FTP Server"
                    strCompleteFTPPath += strFileName;
        
                    try
                    {
                        Byte[] buffer = new Byte[streamObj.Length];
                        streamObj.Read(buffer, 0, buffer.Length);
                        streamObj.Close();
                        streamObj = null;
                                        
        
                        FtpWebRequest requestObj = FtpWebRequest.Create(strCompleteFTPPath) as FtpWebRequest;
                        requestObj.KeepAlive = false;
                        requestObj.Method = WebRequestMethods.Ftp.UploadFile;
                        requestObj.Credentials = new NetworkCredential(strFTPUserName, strFTPPassword);
                        Stream requestStream = requestObj.GetRequestStream();
                        requestStream.Write(buffer, 0, buffer.Length);
                        requestStream.Flush();
                        requestStream.Close();                
                        
        
                       return true;
                    }           
                    catch (Exception ex)
                    {
                        return false;
                    }
        }

        Hope this helps.

        Bharath Vasireddy
        Last edited by Rabbit; Apr 29 '13, 07:49 PM. Reason: Please use code tags when posting code.

        Comment

        Working...