6 Replies - 29476 Views - Last Post: 12 January 2010 - 05:41 PM Rate Topic: -----

#1 balakit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-November 08

File Upload to server

Posted 12 December 2008 - 12:24 AM

Hi,
I need to upload file in a folder at FTP server,

The code i used is,
FileInfo filename = new FileInfo(txtfilurl.Text);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(txtaddr.Text + "/" + filename.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(txtuser.Text, txtpass.Text);
request.KeepAlive = false;
request.UseBinary = true;
Stream ftpstream = request.GetRequestStream();//-->This line shows the error 
FileStream file = File.OpenRead(txtfilurl.Text);

int len = 1024;
byte[] buff = new byte[len];
int bytesread = 0;
do
{
bytesread = file.Read(buff, 0, len);
ftpstream.Write(buff, 0, bytesread);
}
while (bytesread != 0);
file.Close();
ftpstream.Close();


Error msg is:Web exception
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

Is This A Good Question/Topic? 0
  • +

Replies To: File Upload to server

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: File Upload to server

Posted 12 December 2008 - 06:43 AM

View Postbalakit, on 12 Dec, 2008 - 02:24 AM, said:

Hi,
I need to upload file in a folder at FTP server,

The code i used is,
FileInfo filename = new FileInfo(txtfilurl.Text);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(txtaddr.Text + "/" + filename.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(txtuser.Text, txtpass.Text);
request.KeepAlive = false;
request.UseBinary = true;
Stream ftpstream = request.GetRequestStream();//-->This line shows the error 
FileStream file = File.OpenRead(txtfilurl.Text);

int len = 1024;
byte[] buff = new byte[len];
int bytesread = 0;
do
{
bytesread = file.Read(buff, 0, len);
ftpstream.Write(buff, 0, bytesread);
}
while (bytesread != 0);
file.Close();
ftpstream.Close();


Error msg is:Web exception
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).


wouldn't it be a backslash instead of a forward slash?

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(txtaddr.Text + @"\" + filename.Name);


Was This Post Helpful? 0
  • +
  • -

#3 balakit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-November 08

Re: File Upload to server

Posted 12 December 2008 - 10:13 PM

Thanks for ur reply, i will try it... :)

This post has been edited by balakit: 12 December 2008 - 10:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: File Upload to server

Posted 12 December 2008 - 10:53 PM

Actually when setting the server and folder for the FtpWebRequest it needs to be a forward slash, and not an escaped (like we do with backslashes "\\", this will cause major issues).

I notice in your code you use

request.UseBinary = true;



This should only be set to true when uploading binary data, such as an image. Take a look at this code I whipped up and see if it helps solve your issue (since it appears you're uploading an image I kept UseBinary = true)

/// <summary>
/// method for uyploading a file to a specified folder on an
/// FTP server using the FtpWebRequest class
/// </summary>
/// <param name="file">the file to upload</param>
/// <param name="host">the host we're uploading to</param>
/// <param name="username">our login username</param>
/// <param name="password">our login password</param>
/// <param name="folderToUploadTo">the folder we're uploading to</param>
/// <returns></returns>
public bool UploadFileToFtp(string file, string host, string username, string password, string folderToUploadTo)
{
    try
    {
        //create an FtpRequest object
        FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(host + "/" + Path.GetFileName(folderToUploadTo)));
        //set the method to Upload, since we're uploading a file
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
        //pass in our login credentials
        ftpRequest.Credentials = new NetworkCredential(username, password);
        //don't use passive mode
        ftpRequest.UsePassive = true;
        //unless we're uploading a file like an image
        //we need to set this to false as we're not uploading
        //binary data
        ftpRequest.UseBinary = true;
        //set KeepAlive to false
        ftpRequest.KeepAlive = false;

        //now create a new FileStream, then open the file we're uploading
        //this allows us to get the size of the file for our buffer
        FileStream stream = File.OpenRead(file);
        //create a byte[] array buffer the size of the file
        //we're uploading
        byte[] buffer = new byte[stream.Length];

        //read in our file into the FileStream
        stream.Read(buffer, 0, buffer.Length);
        //close the FileStream
        stream.Close();

        //create a new stream, this will be used to write to the
        //FTP server
        Stream requestStream = ftpRequest.GetRequestStream();
        //write the data to the FTP server
        requestStream.Write(buffer, 0, buffer.Length);
        //close the stream
        requestStream.Close();
        //since we made it this far return true
        return true;
    }
    catch (Exception ex)
    {
        //something went wront so let the user know
        MessageBox.Show("Error uploading file: " + ex.Message, "Upload Error");
        //return false
        return false;
    }            
}



Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#5 balakit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-November 08

Re: File Upload to server

Posted 13 December 2008 - 01:57 AM

Thanks to PsychoCoder & eclipsed4utoo for replying,
I tried with the changes u suggested but still getting the same error :( i think it may be a permission prob.....
Was This Post Helpful? 0
  • +
  • -

#6 aashhish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-January 10

Re: File Upload to server

Posted 12 January 2010 - 08:14 AM

I also had same problem, but I figured out, where is problem. I just used Fileupload.postedFile as Stream to read.

[Code]
string ftpfullpath = "ftp://" + ftphost + "/" + Path.GetFileName(inputfilepath);
//Create FTP request
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpfullpath));
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.Timeout = 600000;
ftp.ReadWriteTimeout = 600000;

ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftp.UsePassive = true;
ftp.UseBinary = true;
ftp.KeepAlive = false;

Stream requestStream = ftp.GetRequestStream();
const int bufferLength = 5120;
int readBytes = 0;
byte[] buffer = new byte[bufferLength];
do
{
readBytes = FileUpload.PostedFile.InputStream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
}
while (readBytes != 0);
requestStream.Close();

[Code]
Was This Post Helpful? 0
  • +
  • -

#7 finaiized  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: File Upload to server

Posted 12 January 2010 - 05:41 PM

change the CHMOD to your webserver to allow writing to it. You can also try downloading a FTP program and seeing if you can hook into it.

Also, sometimes your FTP user account doesn't have enough permission either.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1