I cannot figure out how to write a .NET Core Web API to support file downloads. Please note: I do not use the ASP.NET Core MVC form to upload files, but through the Servlet / JSP container. This is how my project.json is defined ,
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Npgsql": "3.1.9",
"CoreCompat.Newtonsoft.Json": "9.0.2-beta001",
"Newtonsoft.Json": "9.0.1"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
This is how my Startup is defined ,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using QCService.Models;
namespace QCService
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<ISurveysRepository, SurveysRepository>();
services.AddSingleton<IFeedbacksRepository, FeedbacksRepository>();
services.AddSingleton<IAttachmentsRepository, AttachmentsRepository>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
}
Finally, this is how my Controller is defined ,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using QCService.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using Microsoft.Net.Http.Headers;
namespace QCService.Controllers
{
[Route("api/[controller]")]
public class AttachmentsController : Controller
{
public IAttachmentsRepository AttachmentItems { get; set; }
public AttachmentsController(IAttachmentsRepository attachmentItems)
{
AttachmentItems = attachmentItems;
}
[HttpGet]
public IEnumerable<Attachments> Get()
{
return AttachmentItems.GetAllAttachments();
}
[HttpGet("{id}")]
public Attachments Get(int id)
{
return AttachmentItems.GetAttachment(id);
}
[HttpGet("Feedback/{id}")]
public IEnumerable<Attachments> GetFeedbackAttachments(int id)
{
return AttachmentItems.GetFeedbackAttachments(id);
}
[HttpPost]
public async Task<IActionResult> PostFiles(ICollection<IFormFile> files)
{
try
{
System.Console.WriteLine("You received the call!");
WriteLog("PostFiles call received!", true);
long size = files.Sum(f => f.Length);
var filePath = Path.GetTempFileName();
var fileName = Path.GetTempFileName();
foreach (var formFile in files)
{
if (formFile.Length > 0)
{
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
}
return Ok(new { count = files.Count, fileName, size, filePath });
}
catch (Exception exp)
{
System.Console.WriteLine("Exception generated when uploading file - " + exp.Message);
WriteLog("Exception generated when uploading file - " + exp.Message, true);
string message = $"file / upload failed!";
return Json(message);
}
}
public void WriteLog(string Message, bool InsertNewLine)
{
LogActivity ologObject = null;
try
{
string MessageString = (InsertNewLine == true ? Environment.NewLine : "") + Message;
if (ologObject == null)
ologObject = LogActivity.GetLogObject();
ologObject.WriteLog(Message, InsertNewLine);
}
catch (Exception ex)
{
Console.WriteLine("Unable to write to the log file : " + ex.Message);
Console.WriteLine("Stack Trace : " + ex.StackTrace);
}
}
}
}
I went through this link,
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads
but just can't make it work! Any help is appreciated.
I use the Google Advanced Rest Client to publish data as follows:
This is how I submit a POST request
...
:
500 -
: 62
5
2
0
Content-Type: multipart/form-data; = ---- WebKitFormBoundary0RKUhduCjSNZOEMN
-: 9106
POST/api/ HTTP/1.1
HOST: localhost: 52770
content-type: multipart/form-data; = ---- WebKitFormBoundary0RKUhduCjSNZOEMN
: 9106
------ WebKitFormBoundary0RKUhduCjSNZOEMN
Content-Disposition: form-data; name= "fileUpload1"; = "1i4ymeoyov_In_the_Wild_Testing.png"
Content-Type: image/png
PNG
IHDR, 3 (tEXtSoftwareAdobe ImageReadyq e < iTXtXML: com.adobe.xmp 8
^ 2IDATx ] pU@a H Pe P8 Θ b Μ3 p q * 7 " h + Yf O atD (< h|DLXdOH =} } ov8_U.....
------ WebKitFormBoundary0RKUhduCjSNZOEMN -