TAILIEUCHUNG - ASP.NET 4 Unleased - p 22

Sử dụng điều khiển Rich Các trang web bao gồm một phương thức có tên CheckFileType (), trong đó ngăn người dùng tải lên một tập tin mà không có gif., jpeg, jpg, hoặc. png mở rộng. Phương pháp này hạn chế các loại tập tin có thể được tải lên dựa trên phần mở rộng tập tin. | 184 CHAPTER 4 Using the Rich Controls The page includes a method named CheckFileType which prevents users from uploading a file that does not have the .gif .jpeg .jpg or .png extension. The method restricts the type of file that can be uploaded based on the file extension. NOTE The HTML specifications define an accept attribute that you can use to filter the files that can be uploaded. Unfortunately no browser supports the accept attribute so you must perform filtering on the server or use some JavaScript to check the filename extension on the client . To save a file to the file system the Windows account associated with the page must have sufficient permissions to save the file. For Windows 2003 and Windows 2008 servers an page executes in the security context of the NETWORK SERVICE account. In the case of every other operating system an page executes in the security context of the ASPNET account. To enable Framework to save an uploaded file to a particular folder you need to right-click the folder within Windows Explorer select the Security tab and provide either the NETWORK SERVICE or ASPNET account Write permissions for the folder see Figure . FIGURE Adding Write permissions for the ASPNET account. From the Library of Wow eBook Accepting File Uploads 185 Saving Files to a Database You also can use the FileUpload control to save files to a database table. Saving and retrieving files from a database can place more stress on your server. However it does have certain advantages. First you can avoid file system permissions issues. Second saving files to a database enables you to more easily back up your information. The page in Listing enables you to save Microsoft Word documents to a database table see Figure . LISTING @ Page Language C @ Import Namespace DOCTYPE html PUBLIC - W3C DTD XHTML Transitional EN http TR xhtml1 DTD script runat