none
行動裝置 FTP傳送圖片的問題 RRS feed

  • 問題

  • 您好~

    想請問一下,我想把PDA拍照後的圖片上傳到伺服器端(FTP),之前只有寫過網頁上傳的方式,我要怎麼取得我拍照的檔案並且上傳到ftp?

    這邊有找一些相關的資訊,也看過這邊一文章,但還是不知道怎麼解決,可以請各位指導一下嗎?

    我在webservice這邊增加一個上傳ftp的方法

    public static bool ftp_Upload(string fileName, string uploadUrl, string UserName, string Password)
            {
    
                Stream requestStream = null;
                FileStream fileStream = null;
                FtpWebResponse uploadResponse = null;
                try
                {
                    FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uploadUrl);
                    uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;//設定Method上傳檔案
                    uploadRequest.Proxy = null;
    
                    if (UserName.Length > 0)//如果需要帳號登入
                    {
                        NetworkCredential nc = new NetworkCredential(UserName, Password);
                        uploadRequest.Credentials = nc; //設定帳號
                    }
    
                    requestStream = uploadRequest.GetRequestStream();
                    fileStream = File.Open(fileName, FileMode.Open);
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while (true)
                    {//開始上傳資料流
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                            break;
                        requestStream.Write(buffer, 0, bytesRead);
                    }
    
                    requestStream.Close();
                    uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
                    return true;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (uploadResponse != null)
                        uploadResponse.Close();
                    if (fileStream != null)
                        fileStream.Close();
                    if (requestStream != null)
                        requestStream.Close();
                }
            }

    在PDA這邊拍照後存到

    ProductWebService.ProductMS MyWS = new PDAWebServiceDemo.ProductWebService.ProductMS();
    
    MyWS.Timeout = 3000;
                MyWS.Url = @weburl;
    
    MyWS .ftp_upload(("~/" + pictureBox1.Image), "ftp://" + FtpUrl + "/Upload/Index//" + pictureBox1.Image, FtpUser, FtpPwd);


    2015年6月13日 上午 04:18

解答

所有回覆