Html页面代码简单,这里不省去。
Default.aspx.cs(上传)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.fileUpload.PostedFile.FileName == "")
{
Response.Write("<script language=javascript>alert(''不能上传空文件'')</script>");
return;
}
SqlConnection con = new SqlConnection("server=17aspx.com;database=download;uid=sa;pwd=17aspx");
con.Open();
try
{
string path = Server.MapPath("upload/");//保存上传文件的文件夹upload虚拟路径对应的实际路径
string filePath = this.fileUpload.PostedFile.FileName;//客户端文件的完全限定名
string serverPath = path + filePath.Substring(filePath.LastIndexOf("\\") + 1);//上传的文件保存在服务器端的路径
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);//文件名
string fileType = this.fileUpload.PostedFile.ContentType.ToString();//文件类型
System.IO.Stream streamFile = this.fileUpload.PostedFile.InputStream;//建立数据流对象
int fileLength = this.fileUpload.PostedFile.ContentLength;//文件长度以字节为单位
byte[] fileData = new Byte[fileLength];//新建一个数组
streamFile.Read(fileData, 0, fileLength);//将这个数据流读取到数组中
SqlCommand cmd = new SqlCommand("insertFile", con);//启用存储过程
cmd.CommandType = CommandType.Sto |