# Download and upload image files

**URL:** <https://community.grafana.com/t/download-and-upload-image-files/95846>\
**Category:** OSS Support\
**Created:** [August 4, 2020, 5:14pm UTC](https://community.grafana.com/t/download-and-upload-image-files/95846 "2020-08-04T17:14:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![masti13harsh](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@masti13harsh](https://community.grafana.com/u/masti13harsh)\
**Post date:** [August 4, 2020, 5:14pm UTC](https://community.grafana.com/t/download-and-upload-image-files/95846/1 "2020-08-04T17:14:53Z")

</div>

I have to perform a basic test where I download file from one url and upload the file to another url.

I tried the following approach:

1. Downloaded the file and stored it in a variable like below:  
fileDownloaded = res.body;
2. Uploaded the file with the response data as:  
file: http.file(fileDownloaded, “default\_report\_image.png”, “b”)

I am getting some file format issue when I open the file in my destination.

---

<div class="post-metadata">

**Author:** ![anon41036752](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@anon41036752](https://community.grafana.com/u/anon41036752)\
**Post date:** [August 4, 2020, 7:55pm UTC](https://community.grafana.com/t/download-and-upload-image-files/95846/2 "2020-08-04T19:55:36Z")

</div>

Hi @masti13harsh,

set `responseType` to `binary` in your request, so that k6 returns a byte array in response:

> **[Params](https://k6.io/docs/javascript-api/k6-http/params/)**
>
> Used for setting various HTTP request-specific parameters such as headers, cookies, etc.

---

<div class="post-metadata">

**Author:** ![masti13harsh](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@masti13harsh](https://community.grafana.com/u/masti13harsh)\
**Post date:** [August 5, 2020, 7:43am UTC](https://community.grafana.com/t/download-and-upload-image-files/95846/3 "2020-08-05T07:43:13Z")

</div>

The response type is binary.  
While uploading the file again, the file gets corrupted

---

<div class="post-metadata">

**Author:** ![anon41036752](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@anon41036752](https://community.grafana.com/u/anon41036752)\
**Post date:** [August 5, 2020, 11:08am UTC](https://community.grafana.com/t/download-and-upload-image-files/95846/4 "2020-08-05T11:08:15Z")

</div>

In order to test your case, I’ve made a simple app in Flask with Python, to act as a webserver for download and upload:

```python
from flask import Flask, render_template, request, send_from_directory
from werkzeug import secure_filename

app = Flask( __name__ , static_url_path='')

@app.route('/download/<filename>')
def download_file(filename):
    return send_from_directory('./', filename)

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        fp = request.files['file']
        fp.save(secure_filename(fp.filename))
        return 'file uploaded successfully'

if __name__ == ' __main__':
    app.run(debug=True)

```

Next, I have installed and run the above file with python (assuming you already have the Python interpreter and `pip` package manager installed):

```shell
$ pip install flask
$ python app.py

```

Then, I tried downloading a file, named `test.pdf`, and uploading it back, to be named `test1.pdf` on the server:

```js
import http from 'k6/http';

export default function () {
    let params = {
        responseType: 'binary'
    };
    let res = http.get("http://127.0.0.1:5000/download/test.pdf", params);

    let data = {
        file: http.file(res.body, "test1.pdf")
    };

    res = http.post("http://127.0.0.1:5000/upload", data);
    console.log(res.body);
}

```

And it worked like a charm:

```shell
$ k6 run -i 1 --vus 1 --duration 5s test.js

          /\ |‾‾| /‾‾/ /‾/   
     /\ / \ | |_/ / / /    
    / \/ \ | | / ‾‾\  
   / \ | |‾\ \ | (_) | 
  / __________\ |__ | \ __\ \___ / .io

  execution: local
     script: test.js
     output: -

  scenarios: (100.00%) 1 executors, 1 max VUs, 35s max duration (incl. graceful stop):
           * default: 1 iterations shared among 1 VUs (maxDuration: 5s, gracefulStop: 30s)

INFO[0001] file uploaded successfully                   

running (00.5s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs 0.5s/5s 1/1 shared iters

    data_received..............: 11 MB 20 MB/s
    data_sent..................: 11 MB 20 MB/s
    http_req_blocked...........: avg=343.67µs min=322.06µs med=343.67µs max=365.29µs p(90)=360.97µs p(95)=363.13µs
    http_req_connecting........: avg=198.76µs min=198.51µs med=198.76µs max=199µs p(90)=198.95µs p(95)=198.97µs
    http_req_duration..........: avg=233.5ms min=41.29ms med=233.5ms max=425.72ms p(90)=387.28ms p(95)=406.5ms 
    http_req_receiving.........: avg=19.68ms min=243.39µs med=19.68ms max=39.12ms p(90)=35.23ms p(95)=37.17ms 
    http_req_sending...........: avg=76.26ms min=113.16µs med=76.26ms max=152.41ms p(90)=137.18ms p(95)=144.8ms 
    http_req_tls_handshaking...: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s      
    http_req_waiting...........: avg=137.55ms min=2.05ms med=137.55ms max=273.06ms p(90)=245.96ms p(95)=259.51ms
    http_reqs..................: 2 3.79285/s
    iteration_duration.........: avg=474.47ms min=474.47ms med=474.47ms max=474.47ms p(90)=474.47ms p(95)=474.47ms
    iterations.................: 1 1.896425/s

```

I hope this helps you achieve your goal.
