.net文件上传的各种操作及创建文件夹和删除上传的文件方法

aspx中
<tr>
<td align="left" colspan="2" style="height: 27px">
<input id="myFile" name="myFile" style="width:441px; height:23px;" type="file" runat="server"/>
<asp:Button ID="btnupload" runat="server" Text="上传" OnClick="upload_Click" Height="23px" Width="57px"/>
</td>
</tr>
<tr>
<td style="width: 250px; height: 21px; text-align: left" rowspan="4">
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting"
Width="250px" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Font-Size="Small" ShowHeader="false">
<Columns>
<asp:BoundField DataField="id" SortExpression="id" Visible="False">
</asp:BoundField>
<asp:BoundField DataField="filename" SortExpression="filename" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="2">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TocProjectConnectionString1 %>"
SelectCommand="SELECT [id],[filename] FROM [upfile] where projectid=@projectid ORDER BY [id]" DeleteCommand="DELETE FROM upfile WHERE (id = @id)" >
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue"/>
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="projectid" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
aspx.cs中
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//带出填表人
string uid = HttpContext.Current.Session["userid"].ToString();
TextBox4.Text = uid;
GetUsername();
//带出当前年月为申请日期
TextBox3.Text = DateTime.Now.ToString("yyyyMMdd");
//当前庶务单单号--年月日加4位流水号,并自动递增
string yms0 = "", ids0 = "", yms1 = "", ids1 = "";
string kids = "";
yms1 = DateTime.Now.ToString("yyyyMMdd");
ids1 = "0001";
ProjectSer proser = new ProjectSer();
string checksql = "select top 1 projectid from projectmain order by projectid desc";
kids = proser.specfval_find(checksql, "projectid");

if (!string.IsNullOrEmpty(kids))
{
yms0 = kids.Substring(0, 8); ids0 = kids.Substring(8, 4);
}

if (yms0 == yms1)
{
int idi = Convert.ToInt32(ids0) + 1;
if (idi < 10) ids1 = "000" + idi.ToString();
if ((idi >= 10) && (idi < 100)) ids1 = "00" + idi.ToString();
if ((idi >= 100) && (idi < 1000)) ids1 = "0" + idi.ToString();
}
TextBox1.Text = yms1 + ids1;
//提交前提示附件是否上传完整
Button1.Attributes.Add("onclick", "javascript:return confirm('请确定附件上传完整! 执行点击[确定];取消操作点击[取消]')");
}
}
protected void upload_Click(object sender, EventArgs e)
{
if (myFile.PostedFile != null)
{
string filename = myFile.PostedFile.FileName;
int i = filename.LastIndexOf(".");
string newext;
if (i < 0)
{
Label3.Text = "请选择上传文件!";
}
else
{
newext = filename.Substring(i);
if (newext != ".jpg" && newext != ".gif" && newext != ".bmp" && newext != ".txt" && newext != ".doc" && newext != ".xls" && newext != ".pdf")
Label3.Text = "文件格式不对,格式只能为.jpg,.gif,.bmp,.txt,.doc,.xls,.pdf!";
else
{
string projectid = TextBox1.Text;
string path = Server.MapPath(".\\upLoadfile\\" + projectid);
//判断文件夹是否存在
//if (!Directory.Exists(path))
Directory.CreateDirectory(path);
//else
//{
// Directory.Delete(path, true);
// Directory.CreateDirectory(path);
//}

//保存上传文件
string newname = Path.GetFileName(filename);
myFile.PostedFile.SaveAs(path + "\\" + newname);

ProjectSer proser = new ProjectSer();
//判断数据库中是否已存在该上传文件
string sql = "select filename from upfile where projectid='{0}' and filename='{1}'";
sql = string.Format(sql, projectid,newname);
string name=proser.specfval_find(sql,"filename");
//将上传的文件写入数据库
if (name == "")
{
string sqlstr = "insert into upfile values('{0}','{1}')";
proser.ExecuteNonQuery(string.Format(sqlstr, projectid, newname));
gv_reflsh();
}
else
{
string sqlstr = "update upfile set filename='{0}' where projectid='{1}' and filename='{2}'";
proser.ExecuteNonQuery(string.Format(sqlstr, newname, projectid, newname));
gv_reflsh();
}
}
}
}
}
//删除附件
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//删除服务器上的附件
ProjectSer proj = new ProjectSer();
string filename=GridView1.Rows[e.RowIndex].Cells[1].Text;
string projectid = TextBox1.Text;
string path = Server.MapPath(".\\upLoadfile\\" + projectid + "\\" + filename);
File.Delete(path);
//删除数据库中的资料
string SqlStr = "delete from upfile where id = " + GridView1.SelectedIndex;
string ConnStr = ConfigurationManager.ConnectionStrings["TocProjectConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
gv_reflsh();
}
//GridView异动后刷新
protected void gv_reflsh()
{
string proid = TextBox1.Text;
SqlDataSource1.SelectCommand = string.Format("select id,filename from upfile where projectid='{0}' order by id",proid);
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataSet;
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
}
类中
public int ExecuteNonQuery(string cmdText)
{
string connString = System.Configuration.ConfigurationManager.AppSettings["dbconnstr"];

using (OleDbConnection connection = new OleDbConnection(connString))
{
connection.Open();
OleDbCommand cmd = new OleDbCommand(cmdText, connection);
int val = cmd.ExecuteNonQuery();
connection.Close();
return val;
}

}
public string specfval_find(string sqlstr,string specf)
{
string connectionString = System.Configuration.ConfigurationManager.AppSettings["dbconnstr"];
string resultstr = "";
string queryString = "";
queryString = sqlstr;

using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader Reader = command.ExecuteReader();
if (Reader.HasRows)
{
Reader.Read();
resultstr = Reader[specf].ToString();
//resultstr = Reader.GetString(0);
}

Reader.Close();
}
return resultstr;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值