replace GetTempFileName (#3639)
* replace GetTempFileName * replace GetTempFileName * good apppull/3648/head
parent
68d336165a
commit
8376f60ed3
|
@ -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.
|
|
@ -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<IFormFile>` (or `List<IFormFile>`) 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
|
||||
|
|
|
@ -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))
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue