How to upload multiple files(.xls) in k6

Hi All,

I am trying to upload data through an excel file to a http request in k6, where in I am using open() given on below link-

Approach 1-

  1. var filename = csvData[ exec .scenario.iterationInTest] [‘pFileName’]; - Reading unique files sequentially from a CSV file.
    const binFile = open (./${filename}, ‘b’); - Open files(.xls) from csv file.

When I used above 2 functions together in the init context, I am getting below error-
" getting scenario information in the init context is not supported "

Approach 2 - Used open () in default function-

export default function ()
let res;
var filename = csvData[ exec .scenario.iterationInTest] [‘pFileName’];
console. log (filename) ;
const binFile = open (./${filename}, ‘b’);

but getting below error -
“The “Open” function is only available in the init context i.e. global scope”

I have also used-
var filename = csvData[Math. floor (Math. random () * csvData.length)] [‘pFileName’]; - Reading files from CSV randomly.

const binFile = open (./${filename}, ‘b’);

Getting below error -
‘‘GoError: open() can’t be used with files that weren’t previously opened during initialization (__VU==0),’’

Please help me on this asap.


Hi @varshagawande !

Welcome to the community forum! :wave:

Yes, k6 has a restriction for the opening files.

As a workaround, you can try to pre-open files, something like:

const allFilesThatCanBeUsed = [
   "file1.json", "file3.json", "file4.json"
if (__VU == 0) {
   allFilesThatCanBeUsed.forEach((file) => open("./test_cases/" + file));

export default function () {
   // logic

Let me know if that helps

Hi @olegbespalov ,

Thanks for the quick response!!

I want to open files and pass them through a POST API request.
I am not getting how to do that using your logic.

Could you please help me.


Hey @varshagawande ,

The workaround that I provided to you earlier should help to fix the error that you faced:

 GoError: open() can’t be used with files that weren’t previously opened during initialization (__VU==0),’’

The idea is to pre-“open” all files that you are going to use in the init context.

Once you can do the HTTP requests using the Multipart request or Advanced multipart request.

Let me know if that helps

Heyy hi
I’m also trying the same. Did you get any solution for this?

Hello @epictellers36 , welcome to the community forum!

I don’t really see, what you want to do exactly.

If you want to use multiple files from your test script, I recommend using SharedArray.

import { SharedArray } from 'k6/data';

const fileNames = ['test1.txt', 'test2.txt'];

function loadFiles(){
  const dataArray = [];
    for(let i=0; i<fileNames.length; i++){
    return dataArray;

let sharedarr = new SharedArray('sharedarr',loadFiles);

export default async function () {