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

发布于 2024-12-25 04:55:42 字数 2140 浏览 0 评论 0原文

在服务器端 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

四叶草在未来唯美盛开 2025-01-01 04:55:42
HttpPostedFile file = File1.PostedFile;
string sName = file.FileName; // Contains file name
HttpPostedFile file = File1.PostedFile;
string sName = file.FileName; // Contains file name
So要识趣 2025-01-01 04:55:42
File1.PostedFile.FileName 

将为您提供文件名,

请按照其示例

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

File1.PostedFile.FileName 

will give you filename

Please follow example of it

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文