Server Side onClickイベントでのasp.net fileupload文字列の使用。たぶん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を使用してワンクリックイベントで新しいファイルをパスに書き込むため、これは私にとってはうまくいったのです。
それの例に従ってください。 "例外。私が最初に構築しなければならないオブジェクトはありますか? –
@nickgowdy、あなたのFileUploadコントロールはUpdatePanelの中にネストされていますか? – Shai
FileUploadは更新パネルの間にネストされていませんが、そのページで1つ使用しています。私はオリジナルの投稿を更新して、それが役に立ったらページのマークアップ全体を表示しました。 –