none
使用 FtpWebRequest 上傳 *.docx 檔到某個ftp站台,上傳完成後開啟的檔案卻有問題 RRS feed

  • 問題

  • 我需要將檔案上傳到某個 ftp 站台,測試結果發現只要是 *.docx 檔的,上傳完成後,檔案開啟都會有問題,但其他類型的檔案卻正常,不知道各位是否有遇過此問題?附上我的程式碼及開啟 *.docx 檔案的錯誤畫面。跟原始正常上傳的檔案 a1.docx、a2.docx。

    a1.docx:https://onedrive.live.com/edit.aspx?cid=3406c255510d57c7&page=view&resid=3406C255510D57C7!593&parId=3406C255510D57C7!137&app=Word

    a2.docx:https://onedrive.live.com/edit.aspx?cid=3406c255510d57c7&page=view&resid=3406C255510D57C7!592&parId=3406C255510D57C7!137&app=Word

        protected void Page_Load(object sender, EventArgs e)
        {
            var url = "ftp://ftp.xxxx.com.tw/";
            var id = "UserName";
            var pwd = "Password";
            var filename = new string[] { "a1.docx", "a2.docx" };
            for (var i = 0; i < filename.Length; i++)
            {
                Copy(url, filename[i], id, pwd, @"d:\" + filename[i], 6);
            }
        }

        /// <summary>
        /// 上傳至ftp
        /// </summary>
        /// <param name="url">FTP位置</param>
        /// <param name="filename">上傳後的檔名</param>
        /// <param name="id">FTP帳號</param>
        /// <param name="pwd">FTP密碼</param>
        /// <param name="file">上傳的檔案(含完整路徑)</param>
        public static void Copy(string url, string filename, string id, string pwd, string file, int ftpTimeOut)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url + "/" + filename);
            request.Headers = null;
            request.UseBinary = true;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(id, pwd);
            request.Timeout = ftpTimeOut * 1000;
            request.Proxy = null;
            request.KeepAlive = true;

            FileInfo fileInfo = new FileInfo(file);
            int bufferLen = 2048;
            byte[] buffer = new byte[bufferLen];
            FileStream fs = fileInfo.OpenRead();
            Stream requestStream = request.GetRequestStream();
            int contentLen = fs.Read(buffer, 0, bufferLen);
            while (contentLen != 0)
            {
                requestStream.Write(buffer, 0, bufferLen);
                contentLen = fs.Read(buffer, 0, bufferLen);
            }
            requestStream.Close();
            fs.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            response.Close();
        }


    

    2017年11月21日 上午 08:54

解答

  • 謝謝大家,此問題已經解決,修改如下:

        /// <summary>

        /// 上傳至ftp
        /// </summary>
        /// <param name="url">FTP位置</param>
        /// <param name="filename">上傳後的檔名</param>
        /// <param name="id">FTP帳號</param>
        /// <param name="pwd">FTP密碼</param>
        /// <param name="file">上傳的檔案(含完整路徑)</param>
        public static void Copy(string url, string filename, string id, string pwd, string file, int ftpTimeOut)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url + "/" + filename);

            request.Headers = null;
            request.UseBinary = true;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(id, pwd);
            request.Timeout = ftpTimeOut * 1000;
            request.Proxy = null;
            request.KeepAlive = true;

            FileInfo fileInfo = new FileInfo(file);
            //int bufferLen = fileInfo.Length;// 2048;
            byte[] buffer = new byte[fileInfo.Length];

            FileStream fs = fileInfo.OpenRead();
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();
        }

    • 已標示為解答 yu_chuan 2017年11月21日 下午 02:45
    2017年11月21日 下午 02:45