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.