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 ºÍ¿Í»§¶Ë¹¦ÄÜ
ÔÚ±¾½Ì³ÌÖУ¬Äú½« ......
ǰ̨ Èç¹ûÄãÓõÄÊÇ gridview ¾Í°ÑRepeaterÌæ»»³Égridview ¡£Ò»ÑùµÄ¡£¡£
<form id="frm01" action="" method="post" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
& ......
¡¡¡¡ÓÉÓÚÓõÄÊÇVPSÖ÷»ú£¬²»ÖªµÀɶÔÒòSESSION×ÜÊǶªÊ§£¬ÎÞÄλ»Cookies¡£
¡¡¡¡²éÒ»ÏÂMSDN£¬ÔÚASP.NETÓÐÁ½¸öCOOKIES£¬Response.CookiesºÍRequest.Cookies£¬ÎÞÂÛÓÃÄĸö¶¼²»ÐУ¬Ìí¼Ó½øÈ¥ºó¾Í¶ÁÈ¡²»µ½
ºóÀ´·¢ÏÖͨ¹ýResponse.CookiesÌí¼Ó¡¢Request.Cookies¶ÁÈ¡²ÅÐС£ÏÂÃæÊÇÌí¼Ó¡¢¶ÁÈ¡¡¢É¾³ýµÄ´úÂ룺
//Ìí¼Ó
HttpCookie c = ne ......
Õ⼸ÌìѧϰʹÓÃWebPart£¬·¢ÏÖÖÚ¶àÎÊÌ⣬ʹÓõãµÎ¼Ç¼ÈçÏ£¬Í¬¸÷λ¹²Ïí£º
1¡¢WebPartµÄʹÓñØÐë»ùÓÚÒ»¸öͨ¹ýÉí·ÝÑéÖ¤µÄÓû§»á»°¡£
2¡¢WebPartµÄʹÓõĸöÐÔ»¯Ó¦ÓÃÓÚËùÓÐÈ˵ÄÑ¡ÏîĬÈÏÊǽûÓõģ¬¿ÉÒÔͨ¹ýÐÞ¸ÄWeb.configÀ´Íê³É
<webParts>
<personalization>
&nb ......
1.´«Í³WebForm¿ª·¢ÖдæÔÚµÄһЩÎÊÌâ
´«Í³µÄASP.NET¿ª·¢ÖУ¬Î¢ÈíµÄ¿ª·¢ÍŶÓΪ¿ª·¢ÕßÉè¼ÆÁËÒ»¸öÔÚ¿ÉÊÓ»¯Éè¼ÆÆ÷ÖÐÍϷſؼþ£¬±àд´úÂëÏìӦʼþµÄ¿ìËÙ¿ª·¢»·¾³¡£È»¶ø£¬ËüËù´øÀ´µÄ¸ºÃæЧӦÊÇ£º
ÓÉÓڿؼþ·â×°Á˺ܶණÎ÷£¬¿ª·¢ÕߺÜÄÑÁ˽âÕâ±³ºóµÄHTMLÊÇÈçºÎÔË×÷µÄ
ÈÝÒ׵õ½Ò»¸ö°üº¬´óÁ¿ViewStateµÄÒ³Ã棬ʹµÃÒ ......