From 8376f60ed3ca31e4284ae0ae653657e3ac81a711 Mon Sep 17 00:00:00 2001 From: Rick Anderson Date: Thu, 6 Jul 2017 12:23:52 -0600 Subject: [PATCH] replace GetTempFileName (#3639) * replace GetTempFileName * replace GetTempFileName * good app --- aspnetcore/includes/GetTempFileName.md | 1 + aspnetcore/mvc/models/file-uploads.md | 10 +++++++--- .../FileUploadSample/Controllers/ProfileController.cs | 5 +++++ 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 aspnetcore/includes/GetTempFileName.md diff --git a/aspnetcore/includes/GetTempFileName.md b/aspnetcore/includes/GetTempFileName.md new file mode 100644 index 0000000000..d770ec2f8b --- /dev/null +++ b/aspnetcore/includes/GetTempFileName.md @@ -0,0 +1 @@ +**Warning**: The following code uses `GetTempFileName`, which throws an `IOException` if more than 65535 files are created without deleting previous temporary files. A real app should either delete temporary files or use `GetTempPath` and `GetRandomFileName` to create temporary file names. The 65535 files limit is per server, so another app on the server can use up all 65535 files. \ No newline at end of file diff --git a/aspnetcore/mvc/models/file-uploads.md b/aspnetcore/mvc/models/file-uploads.md index 8ba523ed57..cc8a78643a 100644 --- a/aspnetcore/mvc/models/file-uploads.md +++ b/aspnetcore/mvc/models/file-uploads.md @@ -1,18 +1,18 @@ --- -title: File uploads +title: File uploads in ASP.NET Core author: ardalis description: How to use model binding and streaming to upload files in ASP.NET Core MVC. keywords: ASP.NET Core, file upload, model binding, IFormFile, streaming ms.author: riande manager: wpickett -ms.date: 11/10/2016 +ms.date: 7/5/2017 ms.topic: article ms.assetid: ebc98159-a028-4a94-b06c-43981c79c6be ms.technology: aspnet ms.prod: asp.net-core uid: mvc/models/file-uploads --- -# File uploads +# File uploads in ASP.NET Core By [Steve Smith](http://ardalis.com) @@ -66,6 +66,8 @@ public interface IFormFile When uploading files using model binding and the `IFormFile` interface, the action method can accept either a single `IFormFile` or an `IEnumerable` (or `List`) representing several files. The following example loops through one or more uploaded files, saves them to the local file system, and returns the total number and size of files uploaded. +[!INCLUDE [GetTempFileName](../../includes/GetTempFileName.md)] + [!code-csharp[Main](file-uploads/sample/FileUploadSample/Controllers/UploadFilesController.cs?name=snippet1)] Files uploaded using the `IFormFile` technique are buffered in memory or on disk on the web server before being processed. Inside the action method, the `IFormFile` contents are accessible as a stream. In addition to the local file system, files can be streamed to [Azure Blob storage](https://azure.microsoft.com/documentation/articles/vs-storage-aspnet5-getting-started-blobs) or [Entity Framework](https://docs.microsoft.com/ef/core/index). @@ -160,6 +162,8 @@ Since model binding is disabled, the `Upload` action method doesn't accept param The complete `Upload` method is shown below: +[!INCLUDE [GetTempFileName](../../includes/GetTempFileName.md)] + [!code-csharp[Main](file-uploads/sample/FileUploadSample/Controllers/StreamingController.cs?name=snippet1)] ## Troubleshooting diff --git a/aspnetcore/mvc/models/file-uploads/sample/FileUploadSample/Controllers/ProfileController.cs b/aspnetcore/mvc/models/file-uploads/sample/FileUploadSample/Controllers/ProfileController.cs index 82b99e18aa..2ce2f5b693 100644 --- a/aspnetcore/mvc/models/file-uploads/sample/FileUploadSample/Controllers/ProfileController.cs +++ b/aspnetcore/mvc/models/file-uploads/sample/FileUploadSample/Controllers/ProfileController.cs @@ -51,6 +51,11 @@ namespace FileUploadSample.Controllers if (model.AvatarFile.Length > 0) { // don't rely on or trust the FileName property without validation + //**Warning**: The following code uses `GetTempFileName`, which throws + // an `IOException` if more than 65535 files are created without + // deleting previous temporary files. A real app should either delete + // temporary files or use `GetTempPath` and `GetRandomFileName` + // to create temporary file names. var filePath = Path.GetTempFileName(); using (var stream = new FileStream(filePath, FileMode.Create)) {