⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upload.js

📁 一款文件上传程序
💻 JS
字号:
/*
   Warning, do not edit this file unless you know what you are doing.
   If this file has any error, the upload fields may not be displayed.
*/

var maxFields     = 20; // maximum number of fields to display
var defaultFields = 3;  // show this number of fields by default
var fieldsCount   = 0;  // keep track of number of fields
var fieldsNumber  = 0;  // used to give name to fields


/* Display default number of fields */
uploadFunc = function ( )
{
    uplContainer  = getObj ( 'upload_fields_container' );
    fileExistStat = getObj ( 'upload_status_div' );
    uploadFilesTo = getObj ( 'upload_to' );

    if ( !uplContainer )
    {
	return alert ( 'Unable to locate upload_fields_container required to generate input fields.' );
    }
    addUploadFields ( defaultFields );
}

addLoadEvent ( uploadFunc );

function addUploadFields ( iCount )
{
    for ( var i = 0; i < iCount; i++ ) addUploadField ( );
}

function addUploadField ( )
{
	if ( uplContainer && fieldsCount < maxFields )
	{
		var newField = document.createElement ( 'div' );
		newField.style.marginTop = '4px';
		newField.innerHTML = '<input type="file" name="file' + fieldsNumber + '" id="file' + fieldsNumber + '" size="68" onchange="checkSelectTwice(this);checkAllFiles()" />' + ' <input type="button" value="Remove" onclick="removeUploadField(this.parentNode);checkAllFiles();" />';
		uplContainer.appendChild ( newField );
		fieldsCount++;
		fieldsNumber++;


	}
}

function removeUploadField ( oField )
{
	if ( uplContainer )
	{
		uplContainer.removeChild ( oField );
		fieldsCount--;
	}
	if ( fieldsCount == 0 )	addUploadField ( );
}

// Check file input fields
function checkAllFiles ( )
{
	if ( uplContainer && uploadFilesTo )
	{
		fileExistStat.innerHTML = '';
		var inputFields = uplContainer.getElementsByTagName ( 'input' );
		var allFieldsEmpty = true;
		for ( var i = 0; i < inputFields.length; i++ )
		{
			if ( inputFields[i].type == 'file' && inputFields[i].value != '' )
			{
				checkFileExists ( inputFields[i].value, uploadFilesTo.value );
				allFieldsEmpty = false;
			}
		}
		if ( allFieldsEmpty )
		{
			fileExistStat.style.display = 'none';
		}
	}
}

// Check if a file exists and update the status
function checkFileExists ( sFile, sFolder )
{
	var xmlhttp = getXMLHttpObject ( );
	if ( xmlhttp && fileExistStat && sFile != '' )
	{
		xmlhttp.onreadystatechange = function ( )
		{
			if ( xmlhttp.readyState == 4 )
			{
				if ( xmlhttp.status == 200 )
				{
					fileExistStat.innerHTML += xmlhttp.responseText;
					fileExistStat.style.display = fileExistStat.innerHTML == '' ? 'none' : 'block';
				}
			}
		}
		xmlhttp.open ( 'GET', 'upload.php?action=checkfile&folder=' + sFolder + '&file=' + escape ( sFile ), true );
		xmlhttp.send ( null );
	}
}

function checkSelectTwice ( oField )
{
	if ( uplContainer )
	{
		var inputFields = uplContainer.getElementsByTagName ( 'input' );

		for ( var i = 0; i < inputFields.length; i++ )
		{
			if ( inputFields[i].type == 'file'  && inputFields[i].value != '' && inputFields[i].name != oField.name )
			{
				if ( inputFields[i].value == oField.value )
				{
					inputFields[i].style.backgroundColor = '#CCFF80';
					alert ( 'It appears that you have already selected this file. It will be removed.' );
					removeUploadField ( oField.parentNode );
					addUploadField ( );
					inputFields[i].style.backgroundColor = '';
				}
			}
		}
	}
}

function uplValidate ( )
{
	if ( uplContainer )
	{
		var inputFields = uplContainer.getElementsByTagName ( 'input' );
		var allFieldsEmpty = true;
		for ( var i = 0; i < inputFields.length; i++ )
		{
			if ( inputFields[i].type == 'file' && inputFields[i].value != '' )
			{
				allFieldsEmpty = false;
			}
		}
		if ( allFieldsEmpty )
		{
			alert ( 'You did not select any file to upload.' );
			return false;
		}
		return true;
	}
	return false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -