2
System.Web.UI.Request

is not available in .net core so the question is about the alternative of this namespace.

I need to read the file content for upload on server. in version 4.5 of .net, i was doing like HttpPostedFile file = Request.Files["content"]; but now this is not working in .net core. I need to know if there is any alternative of best example of file uploading in .net core please share.

following is my old code.

HttpPostedFile file = Request.Files["content"];
byte[] fileData = null;
                    using (var binaryReader = new BinaryReader(file.InputStream))
                    {
                        fileData = binaryReader.ReadBytes(file.ContentLength);
                    }

                    var fileName = Request.Form["fileName"];

                    FileBusiness.SaveFile(domain, new Guid(token), fileName, fileData, true);
                    Response.Write("Normal file uploaded");

I am asking for the upload file functionality in RC1 which is in think is not available in RC1 but i have found in .net core 1.0 RTM libraries. I need to know if there is any way to upload the file in .net core RC1?

Salman Lone
  • 1,526
  • 2
  • 22
  • 29
  • What middleware are you using? From the docs: ASP.NET Core has a number of architectural changes that result in a much leaner and modular framework. ASP.NET Core is no longer based on System.Web.dll. It is based on a set of granular and well factored NuGet packages. This allows you to optimize your app to include just the NuGet packages you need. https://docs.asp.net/en/latest/intro.html – Murray Foxcroft Aug 08 '16 at 07:35
  • 2
    Possible duplicate of [How to get HttpContext.Current in ASP.NET Core?](http://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core) – Murray Foxcroft Aug 08 '16 at 07:38

1 Answers1

1

If you are using ASP.NET Core and enctype="multipart/form-data" form then for reading uploaded file use IFormFile (var file = Request.Form.Files["fileUpload"]) and its CopyTo, CopyToAsync or OpenReadStream methods.

Dmitry
  • 16,110
  • 4
  • 61
  • 73
  • Thank you for the suggestion, i have tried the http://www.mikesdotnetting.com/article/288/uploading-files-with-asp-net-core-1-0-mvc link and it is succesfully uploading the file in .net core. But the article is on .net core 1.0 release and i am using RC1 in my project. Either i need to upgrade my whole project for this or is there any way that i can use IForm type of library in RC1? – Salman Lone Aug 09 '16 at 10:30
  • I do not recommend you to mix RC1 and RTM libraries. Upgrading to RTM is recommended way, because all 3rdparty libraries you use will move to RTM somewhere, causing your project to fetch more and more core RTM libraries as their dependencies. – Dmitry Aug 09 '16 at 12:33
  • Thank you for the suggestion. I will either stay at RC1 or move totally to RTM. My point was, if there is any way of upload the file on server in RC1 so i can use it otherwise i will have to upgrade the whole project in RTM. One more thing, in RC1 i am using framework dnx451 but i think this frameowrk does not have the support in RTM. am i right? – Salman Lone Aug 10 '16 at 05:41
  • I dont' have RC1 on my machine anymore. You can "Go to declaration" on type that is returned from `Request.Form.Files` in VS and inspect it methods - may be it has something like `SaveAsAsync` or similar. – Dmitry Aug 10 '16 at 08:24
  • Nuget package `Microsoft.AspNetCore.Mvc` [supports](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc/) `net451` target moniker. – Dmitry Aug 10 '16 at 08:25