Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public void FTPFileUpload(string ftpfilepath, string inputfilepath)
    {
        string ftphost = "208.91.199.15";  // your server name or localhost        

        string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("tinu", "pass@123");
        //userid and password for the ftp server to given  

        ftp.KeepAlive = true;
        ftp.UseBinary = true;
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        FileStream fs = File.OpenRead(inputfilepath);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        fs.Close();
        Stream ftpstream = ftp.GetRequestStream();
        ftpstream.Write(buffer, 0, buffer.Length);
        ftpstream.Close();
    }
    private void SaveOnLocal(FileUpload Img)
    {
        string _videopath = Img.FileName;
       
        _videopath = Environment.GetEnvironmentVariable("temp") + "\\" + _videopath;// System.IO.Path.GetFullPath(video.PostedFile.FileName);
        FileIOPermission ioPerm = new FileIOPermission(FileIOPermissionAccess.Write, _videopath);
        ioPerm.Demand();
        Img.SaveAs(_videopath);
       
       FTPFileUpload("/Video/", _videopath);
       
       // System.IO.File.Delete(_videopath);
    }public void FTPFileUpload(string ftpfilepath, string inputfilepath)
    {
        string ftphost = "200.45.459.49";  // your server name or localhost        

        string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("Myusername", "MyPasswd");
        //userid and password for the ftp server to given  

        ftp.KeepAlive = true;
        ftp.UseBinary = true;
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        FileStream fs = File.OpenRead(inputfilepath);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        fs.Close();
        Stream ftpstream = ftp.GetRequestStream();
        ftpstream.Write(buffer, 0, buffer.Length);
        ftpstream.Close();
    }
    private void SaveOnLocal(FileUpload Img)
    {
        string _videopath = Img.FileName;
       
        _videopath = Environment.GetEnvironmentVariable("temp") + "\\" + _videopath;// System.IO.Path.GetFullPath(video.PostedFile.FileName);
        FileIOPermission ioPerm = new FileIOPermission(FileIOPermissionAccess.Write, _videopath);
        ioPerm.Demand();
        Img.SaveAs(_videopath);
       
       FTPFileUpload("/Video/", _videopath);
       
       // System.IO.File.Delete(_videopath);
    }



The requested URI is invalid for this FTP command.
Posted
Comments
fjdiewornncalwe 15-Feb-13 10:27am    
When you run your debugger, what line of the code does the error occur on. Could you please highlight that for us? It will make it much easier to help.
R. Giskard Reventlov 15-Feb-13 12:31pm    
Probably a stupid question but have you put a forwardslash between "ftp://" + ftphost + ftpfilepath so that it would be "ftp://" + ftphost + "/" + ftpfilepath?
Richard C Bishop 15-Feb-13 14:54pm    
It is a valid question, there is not enough info provided to determine what "ftphost" and "ftpfilepath" actually look like.

1 solution

Replace
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);

by
C#
WebRequest ftp = WebRequest.Create(ftpfullpath + FileName);
 
Share this answer
 
v2

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