-1

I'm new to ASP.NET but have some knowledge on using JMeter. My task is to create a web-service which accepts file uploaded via POST request.

My Controller

public class TestController : Controller {

    [HttpPost]
    public string Upload(HttpPostedFileBase file) {
        Logger.Debug("Upload Called, Request.Files.Count: " + Request.Files.Count);

        if(model.file != null)
            Logger.Debug("Okay");
        else
            Logger.Debug("Invalid file");

        return "done";
    }
}

My JMeter sampler JMeter HTTP request sampler The test.csv file and my JMeter test plan are put under the same directory.

I however find the file variable is always null, and Request.Files.Count is always 0. Please help, Thanks in advance!

  • Please check the following answer http://stackoverflow.com/questions/15680629/mvc-4-razor-file-upload – Zehan Jurangpathy Mar 07 '17 at 06:30
  • Possible duplicate of [MVC 4 Razor File Upload](http://stackoverflow.com/questions/15680629/mvc-4-razor-file-upload) – jegtugado Mar 07 '17 at 07:08
  • Thanks first! I have checked that my JMeter request parameter name matches the controller's input parameter name, both called "file". Request.Files.Count is also found always 0, so I think the request contains no file. Would you please suggest my problem? – David Chan Mar 07 '17 at 07:20

2 Answers2

1

The easiest way to mimic file upload request is just recording it using HTTP(S) Test Script Recorder. See Apache JMeter Proxy Step by Step manual for JMeter configuration instructions.

Just make sure your test.csv file is located in JMeter's "bin" folder during recording of the upload request, JMeter will automatically populate the relevant HTTP Request sampler configuration. See Recording File Uploads with JMeter guide for more details.


NB: don't forget to properly handle dynamic parameters like VIEWSTATE

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

I finally get a solution.

C# Controller

public class TestController : Controller {

    [HttpPost]
    public string Upload() {
        Logger.Debug("Upload Called, Request.Files.Count: " + System.Web.HttpContext.Current.Request.Files.Count);

        if(System.Web.HttpContext.Current.Request.Files.Count > 0)
            Logger.Debug("Okay");
        else
            Logger.Debug("Invalid file");

        return "done";
    }
}

Note that System.Web.HttpContext.Current.Request is used instead of System.Web.HttpPostedFileBase, and the JMeter sampler needs not to be changed.