lr录制文件下载的脚本

来源:互联网 发布:淘宝客服岗位职责打包 编辑:程序博客网 时间:2024/05/18 23:24

LR是无法记录文件如何保存到本地的,我们把这个请求写在了一个单独的action中,所以只要把服务器响应的所有内容均获取下来写到本地就完成了下载文件的保存。

web_url("download.php",
"URL=http://211.147.208.141/cn/resources/download.php?id=386",
"Resource=1",
"RecContentType=application/force-download",
"Referer=",
LAST);

Action()
{
 int flen;   //保存文件的大小
 long fileds;   //保存文件的句柄
 char FilePath[256]="\0"; //保存文件路径及名称

web_set_max_html_param_len("10000"); //设置页面接受的最大字节数,应大于下载文件的大小

web_concurrent_start(NULL);
 
 //关联文件内容
 web_reg_save_param("FileContent",
 "LB=",
 "RB=",
 "Search=BODY",
 LAST);

 //关联文件名。有些文件名是常量,所以无需关联
 web_reg_save_param("fileName",
 "LB=filename=\"",
 "RB=\"",
 "Search=all",
 LAST);
 
 //原内容,发出请求
 web_url("download.php",
 "URL=http://211.147.208.141/cn/resources/download.php?id=386",
 "Resource=1",
 "RecContentType=application/force-download",
 "Referer=",
 LAST);

 web_concurrent_start(NULL);

 //将路径追加给指针变量
 strcat(FilePath,"c:\\");
 //将文件名追加给指针变量。转换为字符串。
 strcat(FilePath,lr_eval_string("{FileName}"));

 //获得文件大小
 flen=web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

 if(flen > 0)
 {
  if((fileds = fopen(FilePath,"wb")) == NULL)
  {
   lr_output_message("Open File Failed");
   return -1;
  }
  fwrite(lr_eval_string("{FileContent}"),flen,1,fileds);
 }
 return 0;
}

如需重复保存文件到本地
char FilePath[256]="\0";
char *chNumber;

chNumber=lr_eval_string("{Random}"); //生成随机数

strcat(FilePath,"c:\\");
strcat(FilePath,chNumber);
strcat(FilePath,".rar");

 

 

原创粉丝点击