上传文件的本质是浏览器读取本地文件的内容,以二进制数据方式传输到服务端,服务端新建一个文件,将获取到的数据复制到文件中
LR中上传操作可以通过web_submit_data函数实现,支持录制
要点:
web_add_header(“Content-type”,”multipart/form-data”)或者Enctype=“multipart/form-data”
一般情况需要加 web_add_header(“Content-Type”,”multipart/form-data”);
Action()
{
web_reg_save_param("fin_msg",
"LB=",
"RB=",
"Ord=1",
"Search=Body",
LAST); web_add_header("Content-Type","multipart/form-data"); lr_start_transaction("form-data"); //上传文件---函数 web_submit_data
web_submit_data("Attachments", "Action=http://localhost:8080/pinter/file/api/upload", "Method=POST", "EncType=multipart/form-data", "TargetFrame=", "RecContentType=text/html", "Snapshot=t5.inf", "Mode=HTML", ITEMDATA, "Name=file", "Value=C:\\a.txt", "File=yes",
ENDITEM, LAST ); lr_convert_string_encoding(lr_eval_string("{fin_msg}"), LR_ENC_UTF8, LR_ENC_SYSTEM_LOCALE, "Account"); if(strcmp(lr_eval_string("{Account}"), "上传成功") == ){
lr_end_transaction("form-data", LR_PASS); }else{
lr_end_transaction("form-data", LR_FAIL); } return ;
}
运行后日志如下:
Virtual User Script started at : -- ::
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0. for WINXP; build (Aug ::) [MsgId: MMSG-]
Run Mode: HTML [MsgId: MMSG-]
Run-Time Settings file: "C:\test_huzhenyu\Script\biao_dan_upload\\default.cfg" [MsgId: MMSG-]
Ending action vuser_init.
Running Vuser...
Starting iteration .
Starting action Action.
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): Warning -: The header being added may cause unpredictable results when applied to all ensuing URLs. It is added anyway [MsgId: MWAR-]
Action.c(): web_add_header("Content-Type") highest severity level was "warning" [MsgId: MMSG-]
Action.c(): Notify: Transaction "form-data" started.
Action.c(): t=618ms: -byte response headers for "http://localhost:8080/pinter/file/api/upload" (RelFrameId=, Internal ID=)
Action.c(): HTTP/1.1 \r\n
Action.c(): Content-Type: text/plain;charset=UTF-\r\n
Action.c(): Content-Length: \r\n
Action.c(): Date: Sat, Mar :: GMT\r\n
Action.c(): \r\n
Action.c(): t=643ms: -byte response body for "http://localhost:8080/pinter/file/api/upload" (RelFrameId=, Internal ID=)
Action.c(): 涓婁紶鎴愬姛
Action.c(): Notify: Saving Parameter "fin_msg = 涓婁紶鎴愬姛".
Action.c(): web_submit_data("Attachments") was successful, body bytes, header bytes [MsgId: MMSG-]
Action.c(): Notify: Parameter Substitution: parameter "fin_msg" = "涓婁紶鎴愬姛"
Action.c(): Notify: Saving Parameter "Account = 上传成功\x00".
Action.c(): Notify: Parameter Substitution: parameter "Account" = "上传成功\x00"
Action.c(): Notify: Transaction "form-data" ended with "Pass" status (Duration: 0.6063 Wasted Time: 0.3921).
Ending action Action.
Ending iteration .
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.
log