在asp.net背后的代码中访问类型文件的输入控件
在服务器端 onClick 事件中使用 asp.net 文件上传字符串。
ASP.Net 文件上传控件
<asp:FileUpload ID="btnFileUpload" runat="server" Width="0px" onchange="this.form.lblUploadStatus.value=GetFileName(this.value);"/>
<asp:TextBox ID="txtUploadStatus" runat="server" Width="680px"></asp:TextBox>
Javascript
<script type="text/javascript">
function GetFileName(val) {
var i = val.lastIndexOf("\\");
$get('<%= txtUploadStatus.ClientID %>').value = val;
return true;
}
</script>
.net
using (SqlConnection dbConnection = new SqlConnection(CKS_app_settings.sql_conn_string_db))
{
try
{
dbConnection.Open();
SqlCommand command = new SqlCommand(sSQL, dbConnection);
//command.Transaction = tn;
command.CommandText = sSQL;
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 1024;
// Split entire file path to grab filename
string[] split = txtUploadStatus.Text.Split(new char[] { '\\' });
string fileName = split[06];
command.Parameters.AddWithValue("@p_filename", fileName);
command.Parameters.AddWithValue("@p_url", txtUrl.Text);
command.Parameters.AddWithValue("@p_Title", txtImgTitle.Text);
command.Parameters.AddWithValue("@p_alt_text", txtAlt.Text);
int rowsAffected = command.ExecuteNonQuery();
}
catch (SqlException ex)
{
// throw ex;
//If it failed for whatever reason, rollback the //transaction
//tn.Rollback();
//No need to throw because we are at a top level call and //nothing is handling exceptions
result = ex.InnerException.Message;
}
}
也许有更好的解决方案,但这最终对我有用,因为我想插入数据进入数据库并使用 system.io 通过一键事件将新文件写入路径。
Using an asp.net fileupload string in an Server Side onClick event.
ASP.Net file upload control
<asp:FileUpload ID="btnFileUpload" runat="server" Width="0px" onchange="this.form.lblUploadStatus.value=GetFileName(this.value);"/>
<asp:TextBox ID="txtUploadStatus" runat="server" Width="680px"></asp:TextBox>
Javascript
<script type="text/javascript">
function GetFileName(val) {
var i = val.lastIndexOf("\\");
$get('<%= txtUploadStatus.ClientID %>').value = val;
return true;
}
</script>
.net
using (SqlConnection dbConnection = new SqlConnection(CKS_app_settings.sql_conn_string_db))
{
try
{
dbConnection.Open();
SqlCommand command = new SqlCommand(sSQL, dbConnection);
//command.Transaction = tn;
command.CommandText = sSQL;
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 1024;
// Split entire file path to grab filename
string[] split = txtUploadStatus.Text.Split(new char[] { '\\' });
string fileName = split[06];
command.Parameters.AddWithValue("@p_filename", fileName);
command.Parameters.AddWithValue("@p_url", txtUrl.Text);
command.Parameters.AddWithValue("@p_Title", txtImgTitle.Text);
command.Parameters.AddWithValue("@p_alt_text", txtAlt.Text);
int rowsAffected = command.ExecuteNonQuery();
}
catch (SqlException ex)
{
// throw ex;
//If it failed for whatever reason, rollback the //transaction
//tn.Rollback();
//No need to throw because we are at a top level call and //nothing is handling exceptions
result = ex.InnerException.Message;
}
}
Maybe there was a better solution but this worked for me in the end because I want to insert data into the database and using system.io to write new file to path in one click event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将为您提供文件名,
请按照其示例
http://www. java2s.com/Tutorial/ASP.NET/0080__HTML-Controls/inputtypefile.htm
will give you filename
Please follow example of it
http://www.java2s.com/Tutorial/ASP.NET/0080__HTML-Controls/inputtypefile.htm