上传文件,图片代码
2007-04-11 11:19
405 查看
上传文件,图片代码 CS 类
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace OA.Components
...{
/**//// <summary>
/// Ducument 的摘要说明。
/// </summary>
public class DocumentDB
...{
//<summary>
//添加新的文档 AddDocument
//</summary>
//<return> DocumnetID</return>
public int AddDocument(string _Title,string _Content,int _UserID,string _DuUrl,string _Author,string _FromSource)
...{
Database db = new Database();
SqlParameter [] param = ...{
db.CreateInParam("@Title",SqlDbType.VarChar,200,_Title),
db.CreateInParam("@Content",SqlDbType.Text,8000,_Content),
db.CreateInParam("@UserID",SqlDbType.Int,4,_UserID),
db.CreateInParam("@DocUrl",SqlDbType.VarChar,200,_DuUrl),
db.CreateInParam("@Author",SqlDbType.VarChar,200,_Author),
db.CreateInParam("@FromSource",SqlDbType.VarChar,200,_FromSource)
};
return (db.RunProc("Pr_AddDocument",param));
}
}
}
应用
ASPX 页 HTML代码
<%...@ Page language="c#" Codebehind="AddDocument.aspx.cs" AutoEventWireup="false" Inherits="OA.DesktopModules.Document.AddDocument" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>AddDocument</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="../../Css/Default.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<div class="H1">上传文件:</div>
<DIV class="H3">文件主题:<INPUT type="text" id="Title" name="DocTitle" runat="server">
<asp:RequiredFieldValidator id="rvTitle" runat="server" ErrorMessage="请填写主题" Display="Dynamic" ControlToValidate="Title"></asp:RequiredFieldValidator></DIV>
<DIV class="H3"> </DIV>
<DIV class="H3">文件注释:<INPUT id="Content" type="text" name="DocContent" runat="server"></DIV>
<DIV class="H3"> </DIV>
<DIV class="H3"><FONT face="宋体">文件作者:<INPUT id="Author" type="text" name="txtAuthor" runat="server"></FONT></DIV>
<DIV class="H3"><FONT face="宋体"></FONT> </DIV>
<DIV class="H3"><FONT face="宋体">文件来源:<INPUT id="From" type="text" name="DocCatalog" runat="server"></FONT></DIV>
<DIV class="H3"><FONT face="宋体"></FONT> </DIV>
<DIV class="H3"><FONT face="宋体">文件路径:<INPUT id="FileDir" type="file" name="FileDir" runat="server"></FONT></DIV>
<DIV class="H3"><FONT face="宋体"></FONT> </DIV>
<DIV class="H3"><FONT face="宋体"> </FONT><FONT face="宋体">
<asp:Button id="btAdd" runat="server" Text=" 提 交 "></asp:Button>
<asp:Button id="btClear" runat="server" Text=" 返 回 "></asp:Button></FONT></DIV>
</form>
</body>
</HTML>
CS
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using OA.Components;
namespace OA.DesktopModules.Document
...{
/**//// <summary>
/// AddDocument 的摘要说明。
/// </summary>
public class AddDocument : System.Web.UI.Page
...{
protected System.Web.UI.HtmlControls.HtmlInputText Title;
protected System.Web.UI.HtmlControls.HtmlInputText Content;
protected System.Web.UI.HtmlControls.HtmlInputText Author;
protected System.Web.UI.HtmlControls.HtmlInputText From;
protected System.Web.UI.HtmlControls.HtmlInputFile FileDir;
protected System.Web.UI.WebControls.Button btAdd;
protected System.Web.UI.WebControls.RequiredFieldValidator rvTitle;
protected System.Web.UI.WebControls.Button btClear;
private static string FILEDIR = "/DesktopModules/Document/FileLoad";
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
if (Session["UserID"] == null)
...{
// SystemTools.Encrypt("请先登录!");
Response.Redirect("~/home.aspx");
}
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btAdd.Click += new System.EventHandler(this.btAdd_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private string FileUpload(HtmlInputFile inputFile) //上传文件
...{
string fileName="";
if ((inputFile.PostedFile.ContentLength > 0) &&(inputFile.Value.ToString() != ""))
...{
string fileTaiName =""; //文件后缀
fileTaiName = inputFile.PostedFile.FileName.Substring(inputFile.PostedFile.FileName.LastIndexOf("."));
fileName = inputFile.PostedFile.FileName.Substring(
inputFile.PostedFile.FileName.LastIndexOf("/"),
inputFile.PostedFile.FileName.Length - inputFile.PostedFile.FileName.LastIndexOf("/"));
string fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() +
DateTime.Now.Millisecond.ToString();
fileName = "/" + fileTime + GetRandomint() + fileName.Substring(fileName.IndexOf("."),fileName.Length - fileName.IndexOf("."));
}
if (File.Exists(Server.MapPath(Request.ApplicationPath) + FILEDIR + fileName) == false)
...{
try
...{
if(Directory.Exists(Server.MapPath(Request.ApplicationPath) + FILEDIR) == false )
Directory.CreateDirectory(Server.MapPath(Request.ApplicationPath) + FILEDIR);
inputFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + FILEDIR + fileName);
}
catch (Exception ex)
...{
throw new Exception(ex.Message,ex);
}
}
if (fileName == "")...{return (null);}
else ...{return (FILEDIR + fileName);}
}
private string GetRandomint()
...{
Random random = new Random();
return (random.Next(10000).ToString());
}
private void btAdd_Click(object sender, System.EventArgs e)
...{
if (Title.Value.ToString().Trim().Length > 0)
...{
string docUrl = FileUpload(FileDir);
DocumentDB doc = new DocumentDB();
int docID = doc.AddDocument(Title.Value.ToString(),Content.Value.ToString(),
Int32.Parse(Session["UserID"].ToString()),docUrl,Author.Value.ToString(),From.Value.ToString());
if (docID > 0)
...{
SystemTools.AlertMsg("上传成功!");
}
else
...{
if (File.Exists(Server.MapPath(Request.ApplicationPath)+docUrl) == true)
...{
File.Delete(docUrl);
}
SystemTools.AlertMsg("上传失败!");
}
}
ClearInfo();
}
private void ClearInfo()
...{
Title.Value = "";
Content.Value ="";
Author.Value = "";
From.Value = "";
}
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace OA.Components
...{
/**//// <summary>
/// Ducument 的摘要说明。
/// </summary>
public class DocumentDB
...{
//<summary>
//添加新的文档 AddDocument
//</summary>
//<return> DocumnetID</return>
public int AddDocument(string _Title,string _Content,int _UserID,string _DuUrl,string _Author,string _FromSource)
...{
Database db = new Database();
SqlParameter [] param = ...{
db.CreateInParam("@Title",SqlDbType.VarChar,200,_Title),
db.CreateInParam("@Content",SqlDbType.Text,8000,_Content),
db.CreateInParam("@UserID",SqlDbType.Int,4,_UserID),
db.CreateInParam("@DocUrl",SqlDbType.VarChar,200,_DuUrl),
db.CreateInParam("@Author",SqlDbType.VarChar,200,_Author),
db.CreateInParam("@FromSource",SqlDbType.VarChar,200,_FromSource)
};
return (db.RunProc("Pr_AddDocument",param));
}
}
}
应用
ASPX 页 HTML代码
<%...@ Page language="c#" Codebehind="AddDocument.aspx.cs" AutoEventWireup="false" Inherits="OA.DesktopModules.Document.AddDocument" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>AddDocument</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="../../Css/Default.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<div class="H1">上传文件:</div>
<DIV class="H3">文件主题:<INPUT type="text" id="Title" name="DocTitle" runat="server">
<asp:RequiredFieldValidator id="rvTitle" runat="server" ErrorMessage="请填写主题" Display="Dynamic" ControlToValidate="Title"></asp:RequiredFieldValidator></DIV>
<DIV class="H3"> </DIV>
<DIV class="H3">文件注释:<INPUT id="Content" type="text" name="DocContent" runat="server"></DIV>
<DIV class="H3"> </DIV>
<DIV class="H3"><FONT face="宋体">文件作者:<INPUT id="Author" type="text" name="txtAuthor" runat="server"></FONT></DIV>
<DIV class="H3"><FONT face="宋体"></FONT> </DIV>
<DIV class="H3"><FONT face="宋体">文件来源:<INPUT id="From" type="text" name="DocCatalog" runat="server"></FONT></DIV>
<DIV class="H3"><FONT face="宋体"></FONT> </DIV>
<DIV class="H3"><FONT face="宋体">文件路径:<INPUT id="FileDir" type="file" name="FileDir" runat="server"></FONT></DIV>
<DIV class="H3"><FONT face="宋体"></FONT> </DIV>
<DIV class="H3"><FONT face="宋体"> </FONT><FONT face="宋体">
<asp:Button id="btAdd" runat="server" Text=" 提 交 "></asp:Button>
<asp:Button id="btClear" runat="server" Text=" 返 回 "></asp:Button></FONT></DIV>
</form>
</body>
</HTML>
CS
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using OA.Components;
namespace OA.DesktopModules.Document
...{
/**//// <summary>
/// AddDocument 的摘要说明。
/// </summary>
public class AddDocument : System.Web.UI.Page
...{
protected System.Web.UI.HtmlControls.HtmlInputText Title;
protected System.Web.UI.HtmlControls.HtmlInputText Content;
protected System.Web.UI.HtmlControls.HtmlInputText Author;
protected System.Web.UI.HtmlControls.HtmlInputText From;
protected System.Web.UI.HtmlControls.HtmlInputFile FileDir;
protected System.Web.UI.WebControls.Button btAdd;
protected System.Web.UI.WebControls.RequiredFieldValidator rvTitle;
protected System.Web.UI.WebControls.Button btClear;
private static string FILEDIR = "/DesktopModules/Document/FileLoad";
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
if (Session["UserID"] == null)
...{
// SystemTools.Encrypt("请先登录!");
Response.Redirect("~/home.aspx");
}
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btAdd.Click += new System.EventHandler(this.btAdd_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private string FileUpload(HtmlInputFile inputFile) //上传文件
...{
string fileName="";
if ((inputFile.PostedFile.ContentLength > 0) &&(inputFile.Value.ToString() != ""))
...{
string fileTaiName =""; //文件后缀
fileTaiName = inputFile.PostedFile.FileName.Substring(inputFile.PostedFile.FileName.LastIndexOf("."));
fileName = inputFile.PostedFile.FileName.Substring(
inputFile.PostedFile.FileName.LastIndexOf("/"),
inputFile.PostedFile.FileName.Length - inputFile.PostedFile.FileName.LastIndexOf("/"));
string fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() +
DateTime.Now.Millisecond.ToString();
fileName = "/" + fileTime + GetRandomint() + fileName.Substring(fileName.IndexOf("."),fileName.Length - fileName.IndexOf("."));
}
if (File.Exists(Server.MapPath(Request.ApplicationPath) + FILEDIR + fileName) == false)
...{
try
...{
if(Directory.Exists(Server.MapPath(Request.ApplicationPath) + FILEDIR) == false )
Directory.CreateDirectory(Server.MapPath(Request.ApplicationPath) + FILEDIR);
inputFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + FILEDIR + fileName);
}
catch (Exception ex)
...{
throw new Exception(ex.Message,ex);
}
}
if (fileName == "")...{return (null);}
else ...{return (FILEDIR + fileName);}
}
private string GetRandomint()
...{
Random random = new Random();
return (random.Next(10000).ToString());
}
private void btAdd_Click(object sender, System.EventArgs e)
...{
if (Title.Value.ToString().Trim().Length > 0)
...{
string docUrl = FileUpload(FileDir);
DocumentDB doc = new DocumentDB();
int docID = doc.AddDocument(Title.Value.ToString(),Content.Value.ToString(),
Int32.Parse(Session["UserID"].ToString()),docUrl,Author.Value.ToString(),From.Value.ToString());
if (docID > 0)
...{
SystemTools.AlertMsg("上传成功!");
}
else
...{
if (File.Exists(Server.MapPath(Request.ApplicationPath)+docUrl) == true)
...{
File.Delete(docUrl);
}
SystemTools.AlertMsg("上传失败!");
}
}
ClearInfo();
}
private void ClearInfo()
...{
Title.Value = "";
Content.Value ="";
Author.Value = "";
From.Value = "";
}
}
}
相关文章推荐
- 从iPhone上传图片文件到服务器的代码
- PHP 图片文件上传实现代码
- struts2文件和图片上传代码,很强大的工具类
- java实现图片或文件的上传功能具体思路及代码
- JAVA servlet无刷新上传图片文件并裁剪demo代码(Jcrop)
- iOS Asihttp上传文件(图片等)以及服务端的代码(c#,.net Web api2)
- php实现上传图片文件代码
- spring mvc 多文件 上传 按天生成目录 ,删除,限制为图片代码,实际项目中应用代码
- 图片文件上传入SQL库及显示代码 转
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- 文件、图片的上传(一)-----前端与后端代码
- PHP 图片文件上传实现代码
- android上传图片、视频、大文件代码
- php中检测上传文件类型与上传图片大小代码
- js实现文件上传,图片本地预览(部分代码是借鉴的)
- 从iPhone上传图片文件到服务器的代码
- 图片文件上传入SQL库及显示代码
- php+ajax实现图片文件上传实例代码
- PHP 图片文件上传实现代码
- 图片文件上传代码