IdHTTP 走 HTTPS(SSL) 用 Post 上傳 file 和 text 參數內容 (Delphi 7 + Delphi 2009) |
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
一點小研究, 測試過可以正常上傳檔案
[code delphi] {* ----------------------------------------------------------------- Delphi 7 IdHTTP 走 HTTPS(SSL) 用 Post 上傳檔案和文字參數內容 OpenSSL Library包含有兩個動態鏈接庫libeay32.dll和ssleay32.dll 因為OpenSSL Library中包含有安全方面的一些加密算法, 所以美國政府把它列為禁止出口的產品,所以indy中並沒有帶上這兩個文件 要自己想辦法下載 OpenSSL Library for Indy, 要下載 for Delphi 7 用的 放到和程式相同的目錄中 http://indy.fulgan.com/SSL/ 缺這兩個會出現錯誤訊息 Project Project1.exe raised exception class EIdOSSLCouldNotLoadSSLLibrary with message 'Could not load SSL library.'. Process stopped. Use Step or Run to continue. ----------------------------------------------------------------*} unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdIOHandler, IdIOHandlerSocket, IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, IdMultipartFormData; type TForm1 = class(TForm) IdHTTP1: TIdHTTP; IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocket; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var // Http: TIdHTTP; Stream: TIdMultiPartFormDataStream; FileName : String; begin // Http:=TIdHTTP.Create(nil); IdHTTP1.IOHandler := IdSSLIOHandlerSocket1; // Delphi 7 Stream:=TIdMultiPartFormDataStream.Create; FileName := 'autorunsc.xml'; try // Stream.AddFormField('FieldName', 'FieldValue'); Stream.AddFormField('message', 'messagetext'); // Stream.AddFile('FieldName', FileName, 'FieldType'); Stream.AddFile('uploadedfile', FileName, 'file'); Stream.Position:=0; IdHTTP1.Request.ContentType:=Stream.RequestContentType; IdHTTP1.Post('https://127.0.0.1/fileupload_ok.php', Stream); finally IdHTTP1.Free; Stream.Free; end; Button1.Caption := 'Upload OK'; end; end. [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
Delphi 2009 和 Delphi 7 差不多, 只是元件換了
[code delphi] {* ---------------------------------------------------------------- Delphi 2009 IdHTTP 走 HTTPS(SSL) 用 Post 上傳檔案和文字參數內容 Delphi 7 時代 OpenSSL Library包含有兩個動態鏈接庫libeay32.dll和ssleay32.dll 因為OpenSSL Library中包含有安全方面的一些加密算法, 所以美國政府把它列為禁止出口的產品,所以indy中並沒有帶上這兩個文件 要自己想辦法下載, 放到和程式相同的目錄中 Delphi 2009 時代 不需要這兩個檔案了, 如果 OpenSSL for Delphi 兩個檔案在 程式所在目錄, 會有下面錯誤訊息 Project Project1.exe raised exception class EIdOSSLCouldNotLoadSSLLibrary with message 'Could not load SSL library.'. ---------------------------------------------------------------- Delphi 7 Uses IdMultipartFormData 使用元件 IdHTTP1: TIdHTTP; IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocket; Button1: TButton; Delphi 2009 Uses IdMultipartFormData 使用元件 IdHTTP1: TIdHTTP; Button1: TButton; IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL; ---------------------------------------------------------------- *} unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdIOHandler, IdIOHandlerSocket, IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, IdMultipartFormData, IdIOHandlerStack, IdSSL; type TForm1 = class(TForm) IdHTTP1: TIdHTTP; Button1: TButton; IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var Stream: TIdMultiPartFormDataStream; FileName : String; begin // IdHTTP1 := TIdHTTP.Create; // IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create; // IdHTTP1.IOHandler := IdSSLIOHandlerSocket1; // Delphi 7 IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1; // Delphi 2009 Stream:=TIdMultiPartFormDataStream.Create; FileName := 'autorunsc.xml'; try // Stream.AddFormField('FieldName', 'FieldValue'); Stream.AddFormField('message', 'messagetext'); // Stream.AddFile('FieldName', FileName, 'FieldType'); Stream.AddFile('uploadedfile', FileName, 'file'); Stream.Position:=0; IdHTTP1.Request.ContentType:=Stream.RequestContentType; IdHTTP1.Post('https://127.0.0.1/fileupload_ok.php', Stream); finally IdHTTP1.Free; Stream.Free; end; Button1.Caption := 'Upload OK'; end; end. [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
fileupload_ok.php 程式內容 下面 meta 是連在一起, 不是 met 空白a [code php] <? // FileName : fileupload_ok.php // // 修改php.ini,設定檔案上傳暫存目錄與檔案大小 // file_uploads = On // upload_tmp_dir = // upload_max_filesize = 2M(上傳最大的size) // php.ini 修改後要重新啟動 Apache echo "Message : " . $_POST['message'] . " "; $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); echo "FileName : " . $_FILES['uploadedfile']['name'] . " "; echo "target_path : $target_path "; $userfile = $_FILES['uploadedfile']['name']; $file_size = $_FILES['uploadedfile']['size']; $file_temp = $_FILES['uploadedfile']['tmp_name']; $file_err = $_FILES['uploadedfile']['error']; echo "FileSize : $file_size "; echo "FileTemp : $file_temp "; echo "FileErr : $file_err "; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
因為 delphi 程式傳給 fileupload_ok.php 時候沒有畫面
所以先用 fileupload.php 先測試 fileupload_ok.php 是否正常 下面 meta 是連在一起, 不是 met 空白 a [code php] http://tw.php.net/move_uploaded_file <form enctype="multipart/form-data" action="fileupload_ok.php" method="POST"><br /> Message: <input type=text name="message" /><br /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /><br /></form><br /> [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |