Jmeter和LR上传文件和下载
背景:
在某个申请业务中,需要上传附件文件,然后才能提交该申请
遇到的问题:
1, 在使用Jmeter或者LR进行录制时,无法录制到上传文件的请求,只能通过Fiddler能查看到该请求
2, 因为录制不下来,只能手工构造,但网上相关的材料太少。
所以在这里整理备忘
Fiddler录制得到的请求的Header和Body如下面的两个截屏
body
截图如上,注意Header截图【Content-Type:multipart/form-data;boundary=----------cH2GI3Ef1gL6Ij5cH2gL6Ij5ei4gL6】和Body截图
每一个“-----------------------------”为分隔符,代表一个表单域;
最后一个“----------cH2GI3Ef1gL6Ij5cH2gL6Ij5ei4gL6--”(以“--”结尾)分隔符为表单域结束边界。
由此可以看到我得到的这个请求,POST data中共有四个表单域,最后一个即包含了name=”Filedate”(这个name的值可以自己定),filename=”ff.txt”,下图中红框为ff.txt实际上传的文件内容
Server端的代码如下
一,Jmeter
需要注意
1,请求类型为Post
2,要勾选 Use multipart/form-data for POST,否则request中将不包含上传的文件
3, MIME类型为application/octet-stream
二,LoadRunner
lr_start_transaction("upload"); web_reg_save_param("attachmentId1", "LB/IC=attachmentId\":\"", "RB/IC=\"", "Ord=1", "Search=ALL", LAST); web_submit_data("upload.text", "Action=http://192.168.0.1:8000/fileUpload/upload.text", "Method=POST", "EncType=multipart/form-data", "RecContentType=text/plain", "Referer=", "Snapshot=t230.inf", "Mode=HTTP", ITEMDATA, "Name=Filename","Value=Attachment.doc", ENDITEM, "Name=attachmentType","Value=合同正本", ENDITEM, "Name=Filedata","Value=Attachment.doc", "File=Yes", ENDITEM, "Name=Upload","Value=Submit Query", ENDITEM, LAST); lr_end_transaction("upload",LR_AUTO); |
LoadRunner的下载
lr_start_transaction("downloadApproval"); web_reg_save_param("FileName", "LB=filename=", "RB=.pdf", "Ord=1", LAST); web_url("generateApproval.html", "URL=http:// 192.168.0.1:8000/xx/generateApproval.html?id={PersionId}", "Resource=1", "RecContentType=application/x-msdownload", "Referer=http:// 192.168.0.1:8000/xx/viewWorkflow.html?id={PersionId}", "Snapshot=t60.inf", LAST); lr_message("!!filename is%s.xml",lr_eval_string("{FileName}")); //获取二进制文件类型的文件大小 flen =web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE); lr_message("Size:%d",flen); lr_end_transaction("downloadApproval",LR_AUTO); |