2

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.

2 Answers 2

1
HttpPostedFile file = File1.PostedFile;
string sName = file.FileName; // Contains file name
Sign up to request clarification or add additional context in comments.

10 Comments

I tried this and I recieved a "Object reference not set to an instance of an object." exception. Is there an object I have to construct first?
@nickgowdy, let me guess - your FileUpload control is nested within an UpdatePanel?
My FileUpload isn't nested between an update panel but I am using one on that page. I have updated my original post to show entire markup of page if that helps.
Can you try using <asp:FileUpload ID="File1" runat="server" /> instead of your <input /> ?
Wow I didn't actually know that there was an asp.net file upload control. That makes my life alot easier. Thanks!!
|
0
File1.PostedFile.FileName 

will give you filename

Please follow example of it

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

1 Comment

I tried this and the problem isn't how to access it but there being no value when the onclick event fires in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.