在asp.net后面的代码中访问类型文件的输入控件

时间:2012-01-04 10:54:34

标签: c# javascript asp.net html

在服务器端onClick事件中使用asp.net fileupload字符串。

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>

达网络

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在一次点击事件中将新文件写入路径。

2 个答案:

答案 0 :(得分:1)

HttpPostedFile file = File1.PostedFile;
string sName = file.FileName; // Contains file name

答案 1 :(得分:0)

File1.PostedFile.FileName 

会给你文件名

请按照它的例子

http://www.java2s.com/Tutorial/ASP.NET/0080__HTML-Controls/inputtypefile.htm

相关问题