Steps to be followed.
Add a folder in the Web API application for saving the uploaded files. Go to Solution Explorer > Right-click on Project Name (Web API) > Add > New Folder > Rename folder (here I renamed "uploadFiles").
Add a new controller in our Web API application. Right-click on controller folder (in Web API application) > Add > Controller > Enter controller name(UploadController) > select "Empty API Controller" from Scaffolding options > Add.
Added a new Action into the controller (in Web API application) for uploading the file. I have mentioned the upload file path as mentioned below.
- MultipartFileStreamProvider Class
Suited for writing each MIME body parts of the MIME multipart message to a file using a FileStream.
- MultipartFileData Class
Represents a multipart file data.
Upload multiple files using MultipartFormDataStreamProvider in ASP.NET WebAPI. The concept is based on Multipart/form-data in which we can POST not only multiple file content but also regular form fields which will be available as NameValueCollection on the server side.
In this tutorial, we also see how to override the default behavior of MultipartFormDataStreamProvider which stores the name in a unique BodyPart_{GUID} format to a much more meaningful name. We will also invoke our Web API using Fiddler to POST file data. Alongside, we develop a sample console application which will POST file data using HttpClient class.
Step 3
Add an Action to the HomeController (in MVC Client application) for getting the view of the uploaded file.
Code Ref
- public ActionResult Part5()
- {
- return View();
- }
Step 4
Add View for the Action & Design. Right-click on action method (here, right-click on form action) > Add View... > Enter Name > Add.
Code Ref
- @{
- ViewBag.Title = "Part5";
- }
-
- <style>
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
-
- tr:nth-child(even) {
- background-color: #dddddd;
- }
-
- .button {
- background-color: #4CAF50;
- border: none;
- color: white;
- padding: 15px 32px;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 16px;
- margin: 4px 2px;
- cursor: pointer;
- }
-
- .button4 {
- border-radius: 9px;
- }
- </style>
-
- <fieldset>
-
- <legend style="font-family:Arial Black;color:blue">Upload And Download Files Here</legend>
-
- <div>
- <div class="form-group">
- <div id="updatePanelFile" class="alert" role="alert" style="display:none;">
-
- </div>
- </div>
- <div class="col-md-12" style="text-align:center;margin-bottom:10px;">
- <input type="file" id="file1" class="btn btn-primary" />
- </div>
- <input id="btnPostFile" class="button button4" type="button" value="Upload" />
- </div>
-
- @section Scripts{
- <script>
- $(document).ready(function () {
- $('#btnPostFile').click(function () {
- if ($('#file1').val() == '') {
- alert('Please select file');
- return;
- }
-
- var formData = new FormData();
- var file = $('#file1')[0];
- formData.append('file', file.files[0]);
- $.ajax({
- url: 'http://localhost:47250/api/Upload',
- type: 'POST',
- data: formData,
- contentType: false,
- processData: false,
- success: function (d) {
- $('#updatePanelFile').addClass('alert-success').html('<strong>Upload Done!</strong><a href="' + d + '">Download File</a>').show();
- $('#file1').val(null);
- },
- error: function () {
- $('#updatePanelFile').addClass('alert-error').html('<strong>Failed!</strong> Please try again.').show();
- }
- });
- });
- });
- </script>
- }
- </fieldset>
Code Description
Add the following jQuery using Web API URL for uploading the file to Web API.
- $.ajax({
- url: 'http://localhost:47250/api/Upload',
- type: 'POST',
- data: formData,
- contentType: false,
- processData: false,
- success: function (d) {
- $('#updatePanelFile').addClass('alert-success').html('<strong>Upload Done!</strong><a href="' + d + '">Download File</a>').show();
- $('#file1').val(null);
- }
After file is uploaded successfully, the file link will be available to download using panel.
- success: function (d) {
- $('#updatePanelFile').addClass('alert-success').html('<strong>Upload Done!</strong><a href="' + d + '">Download File</a>').show();
- $('#file1').val(null);
- }
If we face any problem in Web API URL or server issue, then we'll get a warning message as mentioned below.
- error: function () {
- $('#updatePanelFile').addClass('alert-error').html('<strong>Failed!</strong> Please try again.').show();
- }
OUTPUT
Upload the file here.
Below, the panel is showing download file option after successfully uploading in Web API.
This is the path of Web API where files are uploaded with a different name.
Check all uploaded files in Web API URL path.
Summary
- Upload files using Web API and Download files from it.
- Implementation of MultipartFileStreamProvider Class.
- Implementation of MultipartFileData Class.