62,041
社区成员
发帖
与我相关
我的任务
分享
<asp:FileUpload id="IMPORT_FILE" runat="server" Width="50px" Visible ="false" />
<asp:TextBox ID="importFilePath" runat="server" Width="40px" Height = "20px" />
<asp:Button ID="btnImportFile" runat="server" Text="" Width = "30px" OnClick = "btnImportFile_Click" Height = "20px"/>
<asp:FileUpload ID="FileUpload1" runat="server" Width="319px" BackColor="#E0E0E0" />
<asp:TextBox ID="txtDescribe" runat="server" Height="220px" TextMode="MultiLine"
Width="402px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="更 新" Width="76px" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
if (!FileUpload1.HasFile)
{
Response.Write("<script>alert('请指定要上传文件的路径');</script>");
return;
}
else if (FileUpload1.PostedFile.ContentLength == 0)
{
Response.Write("<script>alert('上传文件的内容部能为空!');</script>");
return;
}
else
{
string filename = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\")); //文件名
string jpgOrgif = filename.Substring(filename.Length - 3);//文件格式
jpgOrgif = jpgOrgif.ToLower();//格式判断
string sc = ConfigurationManager.ConnectionStrings["dlink"].ToString();
SqlConnection scon = new SqlConnection(sc);//连接数据库
//我这里是把路径保存进数据库,可以不用理会
string ins = "insert into img(imgurl,name,description) values('productImg" + filename + "','" + txtName.Text.Trim() + "','" + txtDescribe.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(ins, scon);//插入命令
if (!(jpgOrgif.Equals("jpg") || jpgOrgif.Equals("gif")))
{
Response.Write("<script>alert('上传文件格式不正确');</script>");
return;
}
else if (File.Exists(Server.MapPath(Request.ApplicationPath) + "\\productImg" + filename))//判断是否存在同文件
{
Response.Write("<script>alert('该文件在服务器中已经存在,请更改文件名');</script>");
return;
}
else
{
//上面都是些判断
try
{
//上传文件并保存到PRODUCTIMG文件夹
FileUpload1.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\productImg" + filename);
scon.Open();
cmd.ExecuteNonQuery();
scon.Close();
Response.Write("<script>window.alert('更新成功!')</script>");
//想显示在TEXTBOX,那基本没啥可能,TEXTBOX是文本框,顾名思义,
//LISTBOX到时可以显示,我这里用个Literal 作个演示
LiteralPost.text="<img src='"+Server.MapPath(Request.ApplicationPath) + "\\productImg" + filename+"' />"
txtName.Text = "";
txtDescribe.Text = "";
}
catch (Exception ex)
{
Response.Write("<script>alert('由于网络原因,上传失败 " + ex.Message + "')");
}
finally
{
scon.Close();
}
}
}
return;
}
<input id="upfile" type="file" />
<input id="txtfile" type="text" />
<input id="btnup" type="button" />
$(document).ready(function(){
$("#txtfile").click(function(){
$("#upfile").click();
});
});