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
相关文档:
在Web编程过程中,存在着很多安全隐患。比如在以前的ASP版本中,Cookie为访问者和编程者都提供了方便,并没有提供加密的功能。打开IE浏览器,选择“工具”菜单里的“Internet选项”,然后在弹出的对话框里单击“设置”按钮,选择“查看文件”按钮,在弹出的窗口中,就会显示硬盘里 ......
ScriptManager 控件管理支持 AJAX 的 ASP.NET 网页的客户端脚本。默认情况下,ScriptManager 控件会向页面注册 Microsoft AJAX Library 的脚本。这将使客户端脚本能够使用类型系统扩展并支持部分页呈现和 Web 服务调用这样的功能。
必须在页上使用 ScriptManager 控件,以启用下 ......
using System.Web.Mail ;
private void Button1_Click(object sender, System.EventArgs e)
{
//实例化MailMessage对象
System.Web.Mail.MailMessage mail=new System.Web.Mail.MailMessage();
//定义邮件的发送地址 , 可以随便填一个不存在的地址 ......
由于用的是VPS主机,不知道啥原因SESSION总是丢失,无奈换Cookies。
查一下MSDN,在ASP.NET有两个COOKIES,Response.Cookies和Request.Cookies,无论用哪个都不行,添加进去后就读取不到
后来发现通过Response.Cookies添加、Request.Cookies读取才行。下面是添加、读取、删除的代码:
//添加
HttpCookie c = ne ......