BreadcrumbHomeResourcesBlog A Guide To The JMeter File Upload/Download Feature July 18, 2024 A Guide to the JMeter File Upload/Download FeaturePerformance TestingJMeter is one of the most popular load testing tools on the market. When using it, there are a wide array of features available to testers. One of those features is JMeter File Upload/Download.This blog will serve as a step-by-step guide to JMeter File Upload/Download.Time to get started!Table of ContentsUsing JMeter File Upload/DownloadBottom LineTable of Contents1 - Using JMeter File Upload/Download2 - Bottom LineBack to topUsing JMeter File Upload/DownloadFile UploadNow that we have given a brief overview of JMeter performance testing, let us dive into uploading files to JMeter. A form-based File Upload in HTML is described in RFC-1867, which identifies “multipart/form-data” media type for file attachments. JMeter's HTTP Request Sampler in its turn offers a “Use multipart/form-data for POST” checkbox. Ticking this checkbox indicates that the request contains file data. The file(s) needs to be supplied through “Files Upload”' input. In HTML, the source upload form looks like this:method="post" action="UploadServlet" enctype="multipart/form-data"> Welcome to Blazemeter upload test servlet, select file to upload: type="file" name="dataFile" id="fileChooser"/> type="submit" value="Upload" />It is critical to mention “dataFile” as the name of the “file” input, since this must be the parameter that needs to be passed as “Parameter Name”. The HTTP Request should look like the following:Pay attention to the following parameters:File Path: should be the location of the file to upload.Parameter Name: relevant form of “file” type name.MIME Type: the file's Internet media type.The easiest way to get these fields populated for you is using JMeter's Superpower: The HTTP Proxy Server. JMeter is smart enough to detect and populate values during the recording process. Just make sure to copy the file(s) you will be uploading to your JMeter installation’s “bin” folder before uploading the file in the browser, as this is the only way that JMeter can properly capture the request. File Path There are two options for providing a file path:Full path, i.e. c:\somefolder\myFile.txtRelative path, i.e. myFile.txt only When using the full path, JMeter will upload the given file as it is. When providing a relative path such as file name only, JMeter will look for it in the FileServer base folder. You can check the jmeter.log file to see which folder is being investigated by searching for lines that contain “FileServer.”2022-05-01 09:36:59,585 INFO o.a.j.s.FileServer: Default base='C:\jmeter\bin'You can get or set the value using any suitable JSR223 Test Element with Groovy language, such as:import org.apache.jmeter.services.FileServer; FileServer fs = FileServer.getFileServer(); //instantiate FileServer class log.info(fs.getBaseDir()); // get FileServer base dir and write it to jmeter.log fs.setBasedir("/path/to/test/files/location"); // set a new base dirYet it is important to consider the following when choosing a file path:It is preferable to use full paths when running tests locally. Relative paths may differ depending on where the .jmx file is and/or how you launch JMeter.If you are uncertain regarding where to put files, it is recommended to set up a FileServer base.BlazeMeter also simplifies the file path process by automatically detecting any files uploaded with a test script and placing them in a relevant location. You would only need a file name for a CSV Data Set Config or an HTTP Multipart Upload Request. Parameter NameTo make sure your file is going somewhere, it is important to specify the parameter name. There are two ways to determine the correct value: Inspect the page source and get the “name” attribute of the “file” type input. Let JMeter do it for you via the HTTP(S) Test Script Recorder. MIME TypeWhen provided, a MIME type is used as a “Content-Type” request header value. It is important to provide the correct content MIME type, since the application or web server may behave differently based on what content type is being stated and produce an error in case of a mismatch. JMeter's HTTP Proxy server is typically smart enough to guess the MIME type of file being sent. Yet if you are sending files sequentially or in parallel from a test file store you can use a JSR223 Pre-Processor to detect the file MIME Type:import java.nio.file.Files; import java.nio.file.Paths; String mimeType = Files.probeContentType(Paths.get("/path/to/your/file.extension")); // detect file's MIME Type vars.put("mimeType", mimeType); // store file's MIME Type into "mimeType" variableAfter that, you can just type ${mimeType} into the corresponding HTTP Request input.Headers, Cookies, Pre/Post Processors, and ListenersHTTP requests in multipart/form-data mode respect all cookies and headers, just like any other HTTP Sampler. Teams can use pre and post-processors to generate or save dynamic data, as well as see multipart request details in the View Results Tree listener as for any GET/POST/PUT or other requests. Just don't forget to disable or delete these processors when running your real performance test. File DownloadThe File Download action from JMeter perspective is essentially an HTTP GET request. When using a file in the request path it means that the download request is issued and being served. Plus, teams can see reports on related performance metrics. As an example, you can save a response if your use case assumes checking file integrity after being auto-generated or when comparing the content of a downloaded file to another expected file. JMeter offers a Save Responses to a file Listener component to help with this task. AttributeDescriptionRequiredNameDescriptive name for this element that is shown in the tree.NoFilename PrefixPrefix for the generated file names; this can include a directory name. Relative paths are resolved relative to the current working directory (which defaults to the bin/ directory). JMeter versions post 2.4 also support paths relative to the directory containing the current test plan (JMX file). If the path name begins with "~/" (or whatever is in the jmeter.save.saveservice.base_prefix JMeter property), then the path is assumed to be relative to the JMX file location.YesVariable NameName of a variable in which to save the generated file name (so it can be used later in the test plan). If there are sub-samples then a numeric suffix is added to the variable name. E.g. if the variable name is FILENAME, then the parent sample file name is saved in the variable FILENAME, and the filenames for the child samplers are saved in FILENAME1, FILENAME2 etc.NoSave Failed Responses onlyIf selected, then only failed responses are savedYesSave Successful Responses onlyIf selected, then only successful responses are savedYesDon't add number to prefixIf selected, then no number is added to the prefix. If you select this option, make sure that the prefix is unique or the file may be overwritten.YesDon't add suffixIf selected, then no suffix is added. If you select this option, make sure that the prefix is unique or the file may be overwritten.YesThis listener can be placed anywhere in the Test Plan. However, for a Download Scenario we recommend making it a child of the HTTP Request which performs file download so it will be applied only to this specific HTTP Request sampler. It is also possible to use scripting to manipulate response data if you want total control of the File Download action, which is not achievable with the above options.In the JSR223 Post-Processors, Assertions and Listeners there is a prev pre-defined variable . It stands for previous SampleResult class instance and exposes the following functions: getResponseData() which returns the byte array just as “data” variable for Beanshell.getResponseDataAsString() which is a String representation of the parent sampler response data.The above entities can be written to files, stored as JMeter variables, and used in assertions.Back to topBottom LineThe checklist for successful implementation of JMeter File Upload/Download scenarios would be:Check that “Use multipart/form data for POST” is tickedCheck that files assumed for upload do exist in relative location or use full paths Provide correct MIME TypeSave Downloaded files using “Save responses to a file” listener or a script. START TESTING NOWBack to top