62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" style="font-size: 12px;" enctype="multipart/form-data">
备 注:<asp:TextBox ID="markname" runat="server"></asp:TextBox>
上 传:<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClientClick="return checkClint()" Text="上 传"
OnClick="Button1_Click" />
<script language="javascript" type="text/javascript">
function checkClint() {
var getmarkname = document.getElementById("markname");
var getfile = document.getElementById("FileUpload1");
if (getmarkname.value == "") {
alert('请输入图片备注名称!');
getmarkname.focus();
return false;
} else if (getfile.value == "") {
alert('请选择上传文件路径!');
getfile.focus();
return false;
} else {
return true;
}
}
</script>
</form>
</body>
</html>
----------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//获取数据
string getname = this.markname.Text;
string getfile = this.FileUpload1.PostedFile.FileName;
//上传文件
string getlastpath = FileUploadCompant(this.FileUpload1);
//获取上传文件流
byte[] getbyte = new byte[this.FileUpload1.PostedFile.ContentLength];
Stream filestream = this.FileUpload1.PostedFile.InputStream;
//读入数据
filestream.Read(getbyte, 0, this.FileUpload1.PostedFile.ContentLength);
//插入数据
#region
string sql = "insert into employee(markname,markContent,markType,markSize,markLinkUrl) values(@name,@content,@type,@size,@link)";
SqlParameter[] getpars = new SqlParameter[5];
getpars[0] = new SqlParameter("@name", getname);
getpars[1] = new SqlParameter("@content", getbyte);//文件内容插入This.Fileupload1.FileBytes同样可以直接转换成Byte数组不用转换
getpars[2] = new SqlParameter("@type", this.FileUpload1.PostedFile.ContentType);//保存文件类型
getpars[3] = new SqlParameter("@size", this.FileUpload1.PostedFile.ContentLength);//文件长度
getpars[4] = new SqlParameter("@link", getlastpath);
int getrescount = DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.connString, CommandType.Text, sql, getpars);
if (getrescount == 1)
{
//添加成功
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '图片记录成功添加到数据库'); ", true);
}
else
{
//添加失败
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '图片记录添加失败'); ", true);
}
#endregion
}
/// <summary>
/// 文件上传操作-单独隔离出来方法
/// </summary>
public string FileUploadCompant(FileUpload getfileupload)
{
string takeServerpath = string.Empty;//保存到服务器路径
try
{
if (string.IsNullOrEmpty(getfileupload.PostedFile.FileName))
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '请选择上传文件的路径!'); ", true);
getfileupload.Focus();
}
else
{
string filepath = getfileupload.PostedFile.FileName;//获得全路径
//获得文件名
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
//保存服务器路径 -唯一文件名
string serverpath = Server.MapPath("FileuploadDict/") + System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss") + Session.SessionID + filename;
//保存到服务器上
getfileupload.SaveAs(serverpath);
//保存成功 提示信息
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '文件上传成功!'); ", true);
takeServerpath = serverpath;
}
}
catch (Exception se)
{
//抓取一场信息 并提示
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '文件上传失败-上传过程出现如下异常信息:" + se.Message.ToString() + "!'); ", true);
}
return takeServerpath;
}
}