I am using twitter bootstrap framework. I am clicking upload button and then we select a file that need to be uploaded.Then the upload button is changed to change and name of selected file is displayed next to it.But if we are having a big file name i want to limit the file name characters to about 50.How to achieve this ?
<div class="span5">
<div class="fileupload fileupload-new"
data-provides="fileupload">
<span class="btn btn-file" id="uploadRef"> <span class="fileupload-new">Upload
Referral Script</span> <span class="fileupload-exists">Change</span>
<form:input type="file" path="refuploadScript"
onchange="return checkFileExists();" />
</span> <span class="fileupload-preview"></span>
<a href="#"
class="close fileupload-exists" data-dismiss="fileupload"
style="float: none">x</a>
</div>
</div>
For doing this, first hide the default file name display span. Then process the file name and display it separately.
So, to hide the default :
<span class="fileupload-preview" style="display: none;"></span>
on form input change , call the following script function
function truncatefile() // calling a function on change or select
{
var fup = document.getElementById('patientupload'); //store file by ID
var file = fup.files[0]; // store value of file
var filePath = fup.value;
var name;
if(filePath=="")
{
document.getElementById('fname').innerHTML = '';
}
else
{
name=filePath.replace(/^.*[\\\/]/, '');
var ext = filePath.substring(filePath.lastIndexOf('.') + 1);//getting file extension
var fileName = name.substring(0,name.length-4); // storing 0th position till extension begining
var fileNameNew="";
if(fileName.length > 30)
{
var fileNameFst=fileName.substring(0,24); //firstpart of file
var fileNameLst=fileName.substring(fileName.length-5,fileName.length); //last part of file
fileNameNew=fileNameFst+ "...." + fileNameLst+"."+ext; //combine all parts
}
else
{
fileNameNew=fileName+"."+ext; //if length less than 30
}
document.getElementById('fname').innerHTML = fileNameNew;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다