ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 简单文件的上传与保存

简单文件的上传与保存

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-09-23 14:04:27 0 删除 编辑
本文简单地使用Asp.net中FileUpload组件实现简单的文件的上传

页面前台部分

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><asp:FileUpload ID="myFile" runat="server" BorderColor="Silver"   BorderStyle="Solid" BorderWidth="1px" />

附件实体类

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->    /// 
    
/// 附件,加一个实体化类是为了用orm的方式保存附件对象,属性根据自己需要添加
    
/// 

    public class Attachment
    {
        
public int Id { get; set; }

        
public string Name { get; set; }

        
public string ExtendName { get; set; }

        
public long Size { get; set; }

        
public int IsUsed { get; set; }

        
public string Descript { get; set; }

        
public byte[] Content { get; set; }
     }

 

页面后台代码,页面上传按钮的单击事件中添加代码,调用如下函数

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->void saveToDataBase()
{
    
string fileName = myFile.FileName;
    
string fileType = myFile.PostedFile.ContentType;
    
long size = myFile.FileContent.Length;
    
string descript = myDescript.Text;
    
//new一个实体对象
    Attachment attachmentObj = new Attachment();
    attachmentObj.Id 
= 0;
    attachmentObj.Name 
= fileName;
    attachmentObj.ExtendName 
= fileType;
    attachmentObj.Size 
= size;
    attachmentObj.Descript 
= descript;
    attachmentObj.Content 
= myFile.FileBytes;

    
//orm方式保存附件对象,如NHibernate等             
     save(attachmentObj);

    
//其他处理代码,或注册客户端脚本事件或语句
}

 

后面的话:

当然如果想做得比较完美的话,
1)首先在客户端可以添加一些验证,什么文件允许添加,服务器端也要有一些验证。
2)多个文件同时上传等
3)Ajax进度条等

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-462650/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766085