【loadrunner】下载文件方式和方法汇总

1、下载资源不做任何处理,直接下载本地地址

Action()
{
    int iflen,lfbody; //声明变量
     //设置最大长度
     web_set_max_html_param_len("10000");

     //将响应信息存放到fcontent变量
     web_reg_save_param("fcontent", "LB=", "RB=", "SEARCH=BODY", LAST);

     web_url("DownLoadCommonFile",
               "URL=http://{ip}/ResourceSearch/DownLoadCommonFile?url=%5C%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.rar",
               "Resource=1",
              "RecContentType=application/octet-stream",
              "Snapshot=t35.inf",
              LAST);

     //获取响应大小,包括:header+body包大小,如不需header大小,需用strlen函数来统计替换,如:iflen=strlen(lr_eval_string("{fcontent}"));
     iflen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
     if(iflen > 0) 
     {
 //以写方式打开文件,文件存储路径和名称
         if((lfbody = fopen("D:\\%5C%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.rar", "wb")) == NULL)
          {
                      lr_output_message("文件操作失败!");
                     return -1;
           }
       //写入文件内容
       fwrite(lr_eval_string("{fcontent}"), iflen, 1, lfbody);
      //关闭文件
      fclose(lfbody);
     }
return 0;
}


2、下载资源存入全新文件夹(文件夹命名:Resource+时间)
Action()
{
    int status;

    char file[512]="e:\\Resource";   

    int iflen,lfbody; //声明变量

    strcat(file,lr_eval_string("{datatime}"));   //脚本中定义一个datatime参数

    status=mkdir(file); //创建文件夹

     //设置最大长度
     web_set_max_html_param_len("10000");

     //将响应信息存放到fcontent变量
     web_reg_save_param("fcontent", "LB=", "RB=", "SEARCH=BODY", LAST);

     web_url("DownLoadCommonFile",
                  "URL=http://{ip}/ResourceSearch/DownLoadCommonFile?url=%5C%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.rar",
                  "Resource=1",
                  "RecContentType=application/octet-stream",
                  "Snapshot=t35.inf",

                 LAST);

     strcat(file,"\\%5C%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.rar"); //将.rar文件合并至file文件
     //获取响应大小,包括:header+body包大小,如不需header大小,需用strlen函数来统计替换,如:iflen=strlen(lr_eval_string("{fcontent}"));
     iflen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
     if(iflen > 0) 
     {
           //以写方式打开文件,文件存储路径和名称
          if((lfbody = fopen(file, "wb")) == NULL)
          {
                lr_output_message("文件操作失败!");
                return -1;
          }
          //写入文件内容
          fwrite(lr_eval_string("{fcontent}"), iflen, 1, lfbody);
         //关闭文件
         fclose(lfbody);
     }
return 0;
}

3、直接将资源下载本地路径,但资源名称按新名称显示(命名:新资源名称+时间)
Action()
{
    int status;
    char file[512]="e:\\Resource\\";   
    int iflen,lfbody; //声明变量
    status=mkdir(file); //创建文件夹

    strcat(file,lr_eval_string("{datatime}"));   //脚本中定义一个datatime参数

 

     //设置最大长度
     web_set_max_html_param_len("10000");

     //将响应信息存放到fcontent变量
     web_reg_save_param("fcontent", "LB=", "RB=", "SEARCH=BODY", LAST);

     web_url("DownLoadCommonFile",
                   "URL=http://{ip}/ResourceSearch/DownLoadCommonFile?url=%5C%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.rar",
                   "Resource=1",
                   "RecContentType=application/octet-stream",
                  "Snapshot=t35.inf",
                  LAST);

     strcat(file,"%5C%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.rar"); //将.rar文件合并至file文件
     //获取响应大小,包括:header+body包大小,如不需header大小,需用strlen函数来统计替换,如:iflen=strlen(lr_eval_string("{fcontent}"));
     iflen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
     if(iflen > 0) 
     {
           //以写方式打开文件,文件存储路径和名称
           if((lfbody = fopen(file, "wb")) == NULL)
          {
               lr_output_message("文件操作失败!");
               return -1;
          }
         //写入文件内容
        fwrite(lr_eval_string("{fcontent}"), iflen, 1, lfbody);
         //关闭文件
         fclose(lfbody);
     }
return 0;
}

4、直接将资源下载本地路径,但资源名称按新名称显示(命名:新资源名称+时间)
Action()
{
int flen;
long filedes;
char file[256]="D:\\";
char rfile_name[512];
long t;
time(&t);
strcat(file, "downloadfile_");
sprintf(rfile_name,"%d",t);
strcat(file, rfile_name);
strcat(file,".rar");
 

        web_set_max_html_param_len("2000000");   //200000:参数的最大长度,必须大于文件的大小

       web_reg_save_param("fcontent", "LB=", "RB=", "SEARCH=BODY", LAST);

       web_url("DownLoadCommonFile",
                  "URL=http://192.168.192.236:8090/ResourceSearch/DownLoadCommonFile?url=%5C%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.rar",
                  "Resource=1",
                  "RecContentType=application/octet-stream",
                  "Snapshot=t35.inf",
                  LAST);

       //获取响应大小,包括:header+body包大小,如不需header大小,需用strlen函数来统计替换,如:iflen=strlen(lr_eval_string("{fcontent}"));

      flen=web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
      if( flen > 0 );
      {
         if((filedes = fopen(file,"wb"))==NULL)   //以写方式打开文件
         {
                   lr_output_message("打开文件失败!");
                   return -1;
         }
         fwrite(lr_eval_string("{fcontent}"),flen,1,filedes);  //写入文件内容
         fclose(filedes);
     }
return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值