ASP.NET MVC Uploading and Downloading Files
If you come to ASP.NET MVC from a purely ASP.NET Web Forms background, one of the first things you are likely to notice is that all those nice easy Server Controls have disappeared. One of those is the FileUpload, and its absence seems to cause a few problems. This article looks at how to upload files to the server in an MVC world, and how to get them back from the server to the user again.
In Web Forms, when you drag a FileUpload control on to the designer, something happens when the page is rendered which you probably don't notice. The resulting html form that wraps the entire page is decorated with an extra attribute: enctype="multipart/form-data". The FileUpload itself is rendered as an html input type=file. Within an MVC View, there are a number of ways to set this up. The first is with HTML:
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="FileUpload1" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
</form>
Notice that the <form> tag includes the enctype attribute, and method attribute of post. This is needed because the form by default will be submitted via the HTTP get method. The following approach, using the Html.BeginForm() extension method renders the exact same html when the page is requested:
<%
using (Html.BeginForm("", "home", FormMethod.Post, new {enctype="multipart/form-data"}))
{%>
<input type="file" name="FileUpload1" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
<% }%>
Notice the name attribute of the <input type="file"> element. We'll come back to that shortly. In the meantime, the resulting page should look rather blandly like this:
OK. So we can now browse to a local file and click the submit button to upload it to the web server. What is needed next is some way to manage the file on the server. W
Ïà¹ØÎĵµ£º
±¾½Ì³Ì´´½¨Ê¹Óà ASP.NET µÄ AJAX ¹¦ÄܵĻù±¾Ê¾ÀýÓ¦ÓóÌÐò¡£Äú¿ÉÒÔÁ˽⵽ÓÐ¹Ø ASP.NET µÄ AJAX ¹¦Äܵĸü¶àÐÅÏ¢£¬½«ÖªµÀÕâЩ¹¦ÄÜÖ¼ÔÚ½â¾öÄÄЩ¼¼ÊõÎÊÌ⣬ÒÔ¼°ÒÔϽéÉÜÐÔÎĵµ½«Éæ¼°ÄÄÐ©ÖØÒªµÄ AJAX ×é¼þ£º
·ASP.NET AJAX Overview
·Ìí¼Ó AJAX ºÍ¿Í»§¶Ë¹¦ÄÜ
ÔÚ±¾½Ì³ÌÖУ¬Äú½« ......
Äã»á¾³£Åöµ½ÕâÖÖÇé¿ö£º¼¸ºõÔÚÍøÕ¾µÄÿ¸öÒ³ÃæÉÏ,´æ´¢Ò»Ð©È«¾Ö´¦ÀíÐÅÏ¢¡£ÀíÏëµÄ×ö·¨Êǽ«ÕâЩÐÅÏ¢Ò»´ÎÐԵļ¯Öд洢ÔÚ×ÊÁϵµ°¸¿âÖУ¬¶ø²»ÊÇÔÚÍøÕ¾µÄÿ¸öÒ³ÃæÉ϶¼Öظ´ÕâÑùµÄ²Ù×÷¡£±ÈÈç˵Êý¾Ý¿âÁ¬½Ó´®¾ÍÊÇÕâÑùµÄÐÅÏ¢£¬Èç¹ûÕâЩÐÅÏ¢²»ÊǼ¯Öд洢ÔÚÌØ¶¨ÇøÓòÖУ¬¶øÊÇÔÚÍøÕ¾µÄÿ¸öÐèÒªÁ¬½ÓÊý¾Ý¿âµÄÒ³ÃæÉÏÊÖ¹¤ÊäÈë£ ......
------±¾ÎÄ×ªÔØ×ÔÍøÂ磬ËùÓÐȨ¹é×÷ÕßËùÓÐ
Èç¹ûÄãÒѾÓн϶àµÄÃæÏò¶ÔÏ󿪷¢¾Ñé£¬Ìø¹ýÒÔÏÂÕâÁ½²½£º
µÚÒ»²½¡¡ÕÆÎÕÒ»ÃÅ.NETÃæÏò¶ÔÏóÓïÑÔ£¬C££»òVB.NET¡£
ÎÒÇ¿ÁÒ·´¶ÔÔÚûϵͳѧ¹ýÒ»ÃÅÃæÏò¶ÔÏó£¨OO£©ÓïÑÔµÄǰÌáÏÂȥѧASP.NET¡£
ASP.NETÊÇÒ»¸öÈ«ÃæÏò¶ÔÏóµÄ¼¼Êõ£¬²»¶®OO£¬ÄǾø¶Ôѧ²»ÏÂÈ¥£¡
µÚ¶þ²½¡¡¶Ô.NET FrameworkÀà¿âÓÐÒ» ......
¿ÉÒÔʹÓÃÊܱ£»¤ÅäÖÃÀ´¼ÓÃÜ Web Ó¦ÓóÌÐòÅäÖÃÎļþ£¨Èç Web.config
Îļþ£©ÖеÄÃô¸ÐÐÅÏ¢£¨°üÀ¨Óû§ÃûºÍÃÜÂë¡¢Êý¾Ý¿âÁ¬½Ó×Ö·û´®ºÍ¼ÓÃÜÃÜÔ¿£©¡£¶ÔÅäÖÃÐÅÏ¢½øÐмÓÃܺ󣬼´Ê¹¹¥»÷Õß»ñÈ¡Á˶ÔÅäÖÃÎļþµÄ·ÃÎÊ£¬Ò²¿ÉÒÔʹ¹¥»÷ÕßÄÑÒÔ»ñ
È¡¶ÔÃô¸ÐÐÅÏ¢µÄ·ÃÎÊ£¬´Ó¶ø¸Ä½øÓ¦ÓóÌÐòµÄ°²È«ÐÔ¡£
Õë¶Ôasp.net 2.0µÄÓ¦ÓóÌÐòµÄÊý¾Ý¿âÁ´½Ó×Ö·û´®½ ......
ASP.NET »º´æ£º·½·¨ºÍ×î¼Ñʵ¼ù
2009-11-28 16:49
ÕªÒª£ºASP.NET ÌṩÈýÖÖÖ÷ÒªÐÎʽµÄ»º´æ£ºÒ³Ãæ¼¶Êä³ö»º´æ¡¢Óû§¿Ø¼þ¼¶Êä³ö»º´æ£¨»ò³ÆÎªÆ¬¶Î»º´æ£©ºÍ»º´æ API¡£Êä³ö»º´æºÍƬ¶Î»º´æµÄÓŵãÊǷdz£Ò×ÓÚʵÏÖ£¬ÔÚ´ó¶àÊýÇé¿öÏ£¬Ê¹ÓÃÕâÁ½ÖÖ»º´æ¾Í×ã¹»ÁË¡£¶ø»º´æ API ÔòÌṩÁ˶îÍâµÄÁé»îÐÔ£¨Êµ¼ÊÉÏÊÇÏ൱´óµÄÁé»îÐÔ£©£¬¿ÉÓÃÓÚÔÚÓ¦Ó ......