Download ASP.NET 5 File

I am trying to upload a file in ASP.NET 5, but the two methods that I have seen on the Internet fail. With XMLHttpRequest, here is my server side code:

public async Task<ActionResult> UploadSWF() { StreamReader reader = new StreamReader(Request.Body); var text = await reader.ReadToEndAsync(); return View(); } 

[EDIT 1] . And my client side:

 var client = new XMLHttpRequest(); function upload() { var file = document.getElementById("uploadfile"); var formData = new FormData(); formData.append("upload", file.files[0]); client.open("post", "/Home/UploadSWF", true); //client.setRequestHeader("Content-Type", "multipart/form-data"); client.setRequestHeader("Content-Type", "application/x-shockwave-flash"); client.send(formData); } 

But the only thing I can get from this is:

------ WebKitFormBoundaryX1h5stVbtaNe6nFw Content-Disposition: form-data; name = "download"; file name = "data.swf" Content-Type: application / x-shockwave-flash CWS; "

[EDIT 2] : Here is the code I get:

  public ActionResult UploadSWF() { Stream bodyStream = Context.Request.Body; var sr = new StreamReader(bodyStream); var test = sr.ReadToEnd(); return View(); } 

So, I get the file name and content type, but not its contents.

This answer is https://stackoverflow.com/a/3186776/creating the stream to a file, however the file creation part does not work for me, I don’t know what is happening, but Nothing happened. So I tried to do the same with a MemoryStream, and I got an empty string.

So, I tried another way using IFormFile, as shown here: https://github.com/aspnet/Mvc/blob/437eb93bdec0d9238d672711ebd7bd3097b6537d/test/WebSites/ModelBindingWebSite/Controllers/FileUploadControllers.This should be Microsoft's interface. Http, which I added to my project, but I still can’t access it. I do not see any IFormFile in this namespace.

[EDIT 1] . The first method I tried was to use HttpPostedFileBase, as in ASP.NET MVC 5, but it did not work in my vNext project. I always had a MissingMethodException. My client side code:

 <form action="/home/UploadSWF" method="POST" enctype="multipart/form-data"> <input type="file" name="file" accept=".swf, application/x-shockwave-flash/*"> <input type="submit" name="submit" /> </form> 

And in my controller:

 public ActionResult UploadSWF(HttpPostedFileBase file) { return View(); } 
+6
source share
1 answer

IFormFile was introduced as part of the beta3 release. You are probably out of date. Check out project.json and make sure you are using beta versions (or newer ones).

+8
source

Source: https://habr.com/ru/post/982653/


All Articles