次の方法で共有


How to change the default TempFile upload location for the ASP.NET 2.0 FileUpload control?

We used to see a lot memory related issues with File uploading in ASP.NET 1.1 as ASP.NET loads the whole file in memory before saving the file to disk. In ASP.NET 2.0 you can use the FileUpload control which buffers the file on disk while it is being uploaded. This is a great improvement as it does not kill your worker process memory, however if you do not have sufficient space on your disk “C:\” drive then it's likely that you will start seeing those infamous System.OutOfMemoryException's errors again. So the question is where does ASP.NET temporarily buffer the file before it stores it to disk?

Dumping out the HttpRawUploadedContent from a memory dump, I can see that the file is buffered in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\fileupload20\d909613c\d78e07ba\uploads files folder and is called f-zqaz-r.post

0:008> !do 017c3fbc

Name: System.Web.HttpRawUploadedContent+TempFile

MethodTable: 68a870e0

EEClass: 68a87070

Size: 20(0x14) bytes

GC Generation: 0

 (C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll)

Fields:

      MT Field Offset Type VT Attr Value Name

7a754a14 4000fb0 4 ...empFileCollection 0 instance 017c4944 _tempFiles

790fa3e0 4000fb1 8 System.String 0 instance 017c54e4 _filename

790fe3c8 4000fb2 c System.IO.Stream 0 instance 017c55f8 _filestream

0:008> !do 017c54e4

Name: System.String

MethodTable: 790fa3e0

EEClass: 790fa340

Size: 262(0x106) bytes

GC Generation: 0

 (C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)

String: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\fileupload20\d909613c\d78e07ba\uploads\f-zqaz-r.post

If you want to change the default location, The tempDirectory attribute of the <compilation> element allows you to specify the directory to use for temporary file storage during compilation. The default is an empty string (""). In the case of an empty string, and if the current process has the required access permissions, the files are stored in the %FrameworkInstallLocation%\ Temporary ASP.NET Files directory.

For more details on <compilation> element see https://msdn2.microsoft.com/en-us/s10awwz0.aspx

I tested this by creating a folder called “D:\TempFileFolder” and modifying the web.config to <compilation debug="false" tempDirectory="D:\TempFileFolder"/> . The path specified for the files to be uploaded was D:\Uploads. Now running the application uploads the file to D:\uploads and all the temporary files are stored in “D:\TempFileFolder”

Running Filemon you can see the sequence of Disk Access by aspnet_wp.exe. At the end you will notice

“10172 5:27:39 AM aspnet_wp.exe:5636 DELETE D:\TempFileFolder\fileupload2.0\c45dc80b\4a2ce08b\uploads\rcrwfuj5.tmp SUCCESS” which I believe is the buffered file which is deleted after it has been written to D:\uploads.

 

So if you run into a situation where there is not enough disk space on the default "C:\...." Drive as the C:\ drive is continuously accessed by your web app while users are uploading files, you can change the tempDirectory attribute to a different drive or path of your choice.

Comments

  • Anonymous
    October 24, 2006
    Hey Viggy, nice one :) I believe we should also mention that the new folder will require appropriate permissions on the Folder (for the relevant account) which is provided. Cheers, Rahul

  • Anonymous
    July 02, 2007
    Hai vignesh, I am working with a document management system project and in asp.net 2.0 our application was accessed by multiple users. U r article helped me to solve the issue of the C: being filled with huge filesizes when multiusers access our application. Also when i tried to use the asp.net 2.0, i came to know that the FileUpload controlbuffers the file on disk. Actually its the new attribute in httpruntime tag of the web.config which does the trick.. http://msdn2.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.requestlengthdiskthreshold.aspx but as mentioned the RequestLengthDiskThreshold is not taking in bytes.. instead it takes as KB.( I hope so..i am new to .net2.0 ) Thanks a lot to share u r experience.. Continue u r work and have a nice day..

  • Anonymous
    June 12, 2008
    Hi, Thanks for the information but I am not successful in this. I changed the tempDirectory in web.config but it still uses c:systemtemp to temporarily upload the files. Please help. I have also given all required permissions on the folder.

  • Anonymous
    April 03, 2009
    Can you tell me how can i get the name of the abcdef.post file that is created in the C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Filesroot8b33dfb63bad45dbuploads directory, I am in reallt good need for it

  • Anonymous
    February 05, 2011
    The comment has been removed