FtpWebRequest Image Upload - Uploaded File Is Not RecognizableFormat.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jason7069

    FtpWebRequest Image Upload - Uploaded File Is Not RecognizableFormat.

    Hi There.

    I recently implemented a FTP upload feature within an ASP.NET
    application. I use the standard steps that are posted throughout the
    web.

    The basic funcationality works as a file is created on the FTP server
    that is the identical size to what I am uploaded. However, if the
    file is a non-text format (JPG, XLS) the resulting file that is posted
    to FTP is not a valid file type. For example, I uploaded a JPG and
    when I redownload that same JPG my photo software cannot recognize the
    format.

    Here is the code that I am using, as mentioned I feel it's consistent
    with what is posted elsewhere.

    Thanks in advance,
    Jason

    private void Upload(string filename)
    {
    FileInfo fileInf = new FileInfo(filena me);
    string uri = _host + "/" + fileInf.Name;
    FtpWebRequest reqFTP;

    UriBuilder URI = new UriBuilder(uri) ;
    URI.Scheme = "ftp";

    reqFTP = (FtpWebRequest) FtpWebRequest.C reate(URI.Uri);
    reqFTP.Credenti als = new NetworkCredenti al(_user, _password);
    reqFTP.KeepAliv e = false;
    reqFTP.Method = WebRequestMetho ds.Ftp.UploadFi le;
    reqFTP.UseBinar y = true;
    reqFTP.ContentL ength = fileInf.Length;
    reqFTP.UsePassi ve = false;

    int buffLength = 2048;
    byte[] buff = new byte[buffLength];
    int contentLen;

    FileStream fs = fileInf.OpenRea d();

    try
    {
    Stream strm = reqFTP.GetReque stStream();
    contentLen = fs.Read(buff, 0, buffLength);

    while (contentLen != 0)
    {
    strm.Write(buff , 0, contentLen);
    contentLen = fs.Read(buff, 0, buffLength);
    }

    strm.Close();
    fs.Close();
    }
    catch(Exception ex)
    {
    throw;
    }
    }
Working...