繁体   English   中英

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

[英]Accessing input control of type file in code behind asp.net

在服务器端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在一次单击事件中将新文件写入路径。

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

会给你文件名

请按照它的例子

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM