asp.net文件上传下载(转载)

来源:互联网 发布:mac localhost打不开 编辑:程序博客网 时间:2024/06/10 06:03
 

一般来说,文件上传下载对于任何一个网站也是必要的。这里,我在网上参考了下别人的方法,并且自己调试了,详细的写下过程并给出详细的代码。是给自己的一种总结,也是给各位.net新人一个引路吧。其实我也是.net新人。以前一直是看JAVA的。但个人感觉其实编程的道理是一样的。无所谓那种语言。甚至还觉得.net有些方面比JAVA更简便和强大一些。过程如下:
1、如下所示:

2、HttpPostedFile属性和方法:

<Input Type="File" Id="被程序所控制的名称" Runat="Server">

    当一个档案传送到Server 端后,接收和处理的是HtmlInputFile 对象的PostedFile 属性。PostedFile 属性的型态是HttpPostedFile 对象类别,其常用属性如下表所示:
属性                        说明                                                    型态
ContentLength   传回上传文件的长度单位byte           Integer

ContentType     传回上传文件的类型                          String

FileName        传回client端上传文件的名称,
                        具有完整的路径如c:\aa\aa.txt                String

其常用方法如下表所示:
方法                        说明                                                                 语法
SaveAs()   将客户端上传的文件存在服务器的磁盘中   SaveAs(ByVal Filename As String )

3、上传部分代码如下:myFile2是htmlInputfile,要转化为服务器端控件的。

 

protected void Button3_Click(object sender, EventArgs e)
    
{
        HttpPostedFile uploadFile 
= myFile2.PostedFile;
//文件大小        
int upFileLength = uploadFile.ContentLength;
        
byte[] fileArray = new Byte[upFileLength];
        Stream fileStream 
= uploadFile.InputStream;
//读文件到树组里        
fileStream.Read(fileArray, 0, upFileLength);
        
        FileUpLoad fileupload 
= new FileUpLoad();//建立一个文件Model
        fileupload.FileName = uploadFile.FileName;//赋文件名
        fileupload.FileContent = fileArray;//赋文件内容        
UpLoadFileBL.InsertFile(fileupload);上传到数据库

    }

 


4、把数据插入到数据的代码就不用说了。

5、接下来说一下从数据库中下载文件:
先在上图页面中把“下载”变成模板列。如下所示:代码如下:

<asp:TemplateField ShowHeader="False">
                
<ItemTemplate>
                    
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="abc"
                        Text
="下载" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"FileId")%//获取url中的参数值,即通过FileId来下载文件>'></asp:LinkButton>
                
</ItemTemplate>
            
</asp:TemplateField>

双击GridView1的RowCommand事件,并设置CommandName 属性为"abc",在页面文件的代码文件中写下如下代码:

 if (e.CommandName == "abc")
        
{
            GridView1.SelectedIndex 
= Convert.ToInt32(e.CommandArgument);
            Response.Redirect(
"DownLoadFile.aspx?id=" + GridView1.SelectedIndex);
        }

其实CommandArgument获取的值即GridView控件中列项值的。这里就要设置GridView中的属性DataKeyName值了。当然获取这个值是在页面文件中获得的。

接下来应该在建立一个空的下载页面,并写下如下代码:

protected void Page_Load(object sender, EventArgs e)
    
{
        
int id = Convert.ToInt32(Request["id"]);
        
string fileName = null;
        
byte[] attachment = null;
        
try
        
{
            FileUpLoad fileUpLoad 
= UpLoadFileBL.GetFileById(id);//通过文件ID从数据库中取值出来放在Model里。
            fileName = fileUpLoad.FileName;
            attachment 
= fileUpLoad.FileContent;
            Response.ContentType 
= "application/octet-stream";
            Response.AddHeader(
"Content-Disposition""attachment; filename=" + Server.UrlEncode(fileName));
            
int lenth = attachment.Length;
            Response.OutputStream.Write(attachment, 
0, lenth);
            Response.Flush();

        }

        
catch(Exception ex)
        
{ Console.WriteLine(ex.Message); }
    }

这样就可以了。如果有人还有不明白的地方,评论中提出来。。我尽力解答。。

原创粉丝点击