我写的一个文件上传页,文件上传不上去,传数据库文件直接显示:无法显示网页 您正在查找的页当前不可用。 网站可能遇到支持问题,或者您需要 调整您的浏览器设置。前台代码:
<form id="form1" runat="server">
<div>
<input type="file" name="myfile" id="myfileAdd" size="30" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" onserverclick="Button1_ServerClick" />
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
</div>
</form>后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
public partial class inPutData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_ServerClick(object sender, EventArgs e)
{
string img_str = UploadFile(myfileAdd); Label1.Text = img_str;
Response.Write("<script>alert(' 添加成功 !');location.href='inputdata.aspx';</script>");
}
private string UploadFile(System.Web.UI.HtmlControls.HtmlInputFile Fupload)
{
try
{
if (Fupload.PostedFile.FileName == "")
return "";
string filename;
string extname;
if (Fupload.PostedFile.FileName != "")
{
extname = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf(".") + 1).ToUpper();
filename = "printdata" + "."+extname;
Fupload.PostedFile.SaveAs(Server.MapPath("printData\\") + filename);
}
return "添加成功";
}
catch
{
return "添加失败";
}
}
}
<form id="form1" runat="server">
<div>
<input type="file" name="myfile" id="myfileAdd" size="30" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" onserverclick="Button1_ServerClick" />
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
</div>
</form>后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
public partial class inPutData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_ServerClick(object sender, EventArgs e)
{
string img_str = UploadFile(myfileAdd); Label1.Text = img_str;
Response.Write("<script>alert(' 添加成功 !');location.href='inputdata.aspx';</script>");
}
private string UploadFile(System.Web.UI.HtmlControls.HtmlInputFile Fupload)
{
try
{
if (Fupload.PostedFile.FileName == "")
return "";
string filename;
string extname;
if (Fupload.PostedFile.FileName != "")
{
extname = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf(".") + 1).ToUpper();
filename = "printdata" + "."+extname;
Fupload.PostedFile.SaveAs(Server.MapPath("printData\\") + filename);
}
return "添加成功";
}
catch
{
return "添加失败";
}
}
}
解决方案 »
- xys_777 进来接分把`
- WPF 中 DataGrid 的列怎么样可以去除特殊字符呢?
- 如何去掉Treeview自动生成的css样式?
- 这个存储过程如何调用
- 像阿里巴巴网站的企业库换模板是怎么做的呢,模板视作静态的还是动态呢
- *************************请教一个关于Regex.Replace的问题*******************
- 高分求教,问题一解决就散分
- 菜鸟请教——关于客户端弹出对话框
- C#中,如何动态给DataGrid添加模板列,且控件为DropDownList,并关联到数据库。谢谢了。
- 怎样实现弹出小窗口输入用户名和密码?
- 冰天雪地跪地求教
- asp.net如何判断访问wap页面的是手机浏览器还是电脑浏览器
如果你要上传大文件的话,还是建议你使用第三方控件,如
http://www.sogou.com/sogou?query=ajaxupload&pid=AQxRG
特别是这句
Fupload.PostedFile.SaveAs(Server.MapPath("printData\\") + filename);
1.路径对不对(应先检查是否存在该文件夹,不存在则新建)
2.如果这句出错,Label1.Text就是添加失败了,而你又弹出了"添加成功"的窗口...
3.然后,我不知道inputdata.aspx写了些什么
<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="100000" useFullyQualifiedRedirectUrl="false" />
</system.web>
就可以了,谢谢各位 xhbmj思路是正确的,分就他了
/// 上传附件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
if (this.FileUpload1.PostedFile == null || this.FileUpload1.PostedFile.FileName.Trim() == "")
{ Response.Write("<script language=javascript>alert('请选择文件!');</script>"); return;
}
if (this.DropDownList1.SelectedValue == "0")
{
Response.Write("<script language=javascript>alert('请选择文件类型!');</script>"); return; }
string nam = this.FileUpload1.PostedFile.FileName; //取得文件名(抱括路径)里最后一个"."的索引
int i = nam.LastIndexOf(".");
int j = nam.LastIndexOf("\\");
//取得文件扩展名
string exName = nam.Substring(i);
string newFineName = nam.Substring(j + 1);
string WName = nam.Substring(j + 1, i - j - 1);
string v_files1 = System.DateTime.Now.ToString("yyMMddHHmmss") + System.DateTime.Now.Millisecond.ToString() + exName;
string strfile1 = WName.Replace(" ", "").ToString() + "_" + v_files1;
string v_files = strfile1;
if (System.IO.File.Exists(Server.MapPath("Attachment\\" + v_files)))
{ Response.Write("<script language=javascript>alert('服务器中上传文件已存在!');</script>");
return;
} int fileSize = this.FileUpload1.PostedFile.ContentLength / 1024;
if (fileSize >= 10240)
{ Response.Write("<script language=javascript>alert('上传文件不能超过10M!');</script>"); return;
} this.FileUpload1.PostedFile.SaveAs(Server.MapPath("Attachment\\" + v_files));
if (fileSize < 0)
{ Response.Write("<script language=javascript>alert('上传的文件失败!');</script>"); return;
} // HyperLink1.NavigateUrl = "Attachment/" + newFineName;
//this.hylinkText = newFineName;
ViewState["hylinkText"] = v_files; //======================================================================================================== //计算当前publicfileid
int publicfileid = FileQuery.MaxID("qw_publicfile", "publicfileid");
if (publicfileid == -1)
return;
string zdbh = "";
string title = newFineName;
string scope = "";
//edit by lishun 8.11 存ID
string CREATER = Session["UserID"].ToString();
string CATEGORY = "";
int state = 0;
string CONTENT = "";
int v_folderid = int.Parse(this.DropDownList1.SelectedValue.ToString());
string v_approver = "";//批准人?
//string v_files = System.DateTime.Now.ToString("yyMMddHHmmss") + System.DateTime.Now.Millisecond.ToString();
string v_pictures = "";
string filesize = fileSize.ToString() + "k"; string sql = "insert into qw_publicfile " +
" (publicfileid, folderid, filecode, title, pubdate, scope, category, creater, approver, content, pictures, files, createtime, approvetime, passtime, state,filesize,shenhe)" +
" values" +
" (" + publicfileid + ", " + v_folderid + ", '" + zdbh + "', '" + title + "', SYSDATE, " +
" '" + scope + "', '" + CATEGORY + "', '" + CREATER + "', '" + v_approver + "', '" + CONTENT + "'," +
" '" + v_pictures + "', '" + v_files + "', SYSDATE, SYSDATE, SYSDATE, " + state + ",'" + filesize + "',"+state+")"; FileQuery.LowExecSql(sql);
//ViewBind(ViewState["Folderid"].ToString());
BindGridView();
//------插入公文数据
int UserID = int.Parse(Session["UserID"].ToString());
int id = Bulletin.QueryT_B_Bulletin_State_ID();
id++;
Bulletin.InsertbulletinState(id, UserID, 1, publicfileid, 2);
}code]
/// 上传附件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
if (this.FileUpload1.PostedFile == null || this.FileUpload1.PostedFile.FileName.Trim() == "")
{ Response.Write("<script language=javascript>alert('请选择文件!');</script>"); return;
}
if (this.DropDownList1.SelectedValue == "0")
{
Response.Write("<script language=javascript>alert('请选择文件类型!');</script>"); return; }
string nam = this.FileUpload1.PostedFile.FileName; //取得文件名(抱括路径)里最后一个"."的索引
int i = nam.LastIndexOf(".");
int j = nam.LastIndexOf("\\");
//取得文件扩展名
string exName = nam.Substring(i);
string newFineName = nam.Substring(j + 1);
string WName = nam.Substring(j + 1, i - j - 1);
string v_files1 = System.DateTime.Now.ToString("yyMMddHHmmss") + System.DateTime.Now.Millisecond.ToString() + exName;
string strfile1 = WName.Replace(" ", "").ToString() + "_" + v_files1;
string v_files = strfile1;
if (System.IO.File.Exists(Server.MapPath("Attachment\\" + v_files)))
{ Response.Write("<script language=javascript>alert('服务器中上传文件已存在!');</script>");
return;
} int fileSize = this.FileUpload1.PostedFile.ContentLength / 1024;
if (fileSize >= 10240)
{ Response.Write("<script language=javascript>alert('上传文件不能超过10M!');</script>"); return;
} this.FileUpload1.PostedFile.SaveAs(Server.MapPath("Attachment\\" + v_files));
if (fileSize < 0)
{ Response.Write("<script language=javascript>alert('上传的文件失败!');</script>"); return;
} // HyperLink1.NavigateUrl = "Attachment/" + newFineName;
//this.hylinkText = newFineName;
ViewState["hylinkText"] = v_files; //======================================================================================================== //计算当前publicfileid
int publicfileid = FileQuery.MaxID("qw_publicfile", "publicfileid");
if (publicfileid == -1)
return;
string zdbh = "";
string title = newFineName;
string scope = "";
//edit by lishun 8.11 存ID
string CREATER = Session["UserID"].ToString();
string CATEGORY = "";
int state = 0;
string CONTENT = "";
int v_folderid = int.Parse(this.DropDownList1.SelectedValue.ToString());
string v_approver = "";//批准人?
//string v_files = System.DateTime.Now.ToString("yyMMddHHmmss") + System.DateTime.Now.Millisecond.ToString();
string v_pictures = "";
string filesize = fileSize.ToString() + "k"; string sql = "insert into qw_publicfile " +
" (publicfileid, folderid, filecode, title, pubdate, scope, category, creater, approver, content, pictures, files, createtime, approvetime, passtime, state,filesize,shenhe)" +
" values" +
" (" + publicfileid + ", " + v_folderid + ", '" + zdbh + "', '" + title + "', SYSDATE, " +
" '" + scope + "', '" + CATEGORY + "', '" + CREATER + "', '" + v_approver + "', '" + CONTENT + "'," +
" '" + v_pictures + "', '" + v_files + "', SYSDATE, SYSDATE, SYSDATE, " + state + ",'" + filesize + "',"+state+")"; FileQuery.LowExecSql(sql);
//ViewBind(ViewState["Folderid"].ToString());
BindGridView();
//------插入公文数据
int UserID = int.Parse(Session["UserID"].ToString());
int id = Bulletin.QueryT_B_Bulletin_State_ID();
id++;
Bulletin.InsertbulletinState(id, UserID, 1, publicfileid, 2);
}