Sangeetha
Sangeetha

Reputation: 709

Read filename to the textbox from fileupload window

In my asp.net application, I have used Textbox, button and a hidden fileupload control.
When Button is clicked using jquery I am getting the fileupload window as below,

protected void btn_browse_Click(object sender, EventArgs e)
{
    StringBuilder strScript = new StringBuilder();
    strScript.Append("$(document).ready(function(){");
    strScript.Append("$('#FileUpload1').click();");
    strScript.Append("});");
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strScript.ToString(), true);
    txt_fileName.Text=FileUpload1.FileName;
}

My issue is I am unable to show the selected filename from fileupload to the textbox.
The filename is not displaying in the textbox

Any suggessions.

Upvotes: 1

Views: 6278

Answers (1)

Freelancer
Freelancer

Reputation: 9074

On serverside you can do like this:

string filename = Path.GetFileName(fID.PostedFile.FileName);
fID.SaveAs(Server.MapPath("Files/"+filename));
string fpath = "Files/"+filename;

and with jquery:

$(document).ready(function () {  
   $("#btnFileUpload").click(function () {  
        var FUpload = $("#FileUploadControl").val();  
    }
 } 

For JavaScript:

<script type="text/javascript">
function getFileName() {
var varfile = document.getElementById("FileUploadControl");
document.getElementById("filename").value = varfile.value;
}
</script>

FileUpload control will be :

<asp:FileUpload ID="FileUploadControl" runat="server" onchange="getFileName()"

Upvotes: 2

Related Questions