@YouTy6bl4

Загрузка файла без перехода по ссылке, как осуществить?

Добрый день, заметил такую вещь. Когда загружаешь файл т.е. если нажать на специальную кнопку в моём случае "Загрузить" то при отменённом действие "event.preventDefault()" кнопка просто нажимается. Как сделать так, чтобы при нажатии файл загружался на сайт, но при этом не переходил по ссылке?
Вот вам код самой загрузке(+кнопка):
<form action="/lc/index.php" method="post" enctype="multipart/form-data">
  <input type="hidden" name="action" value="skinfile">
  <input type="file" name="skinfile" accept="image/png" id="file">
 <br>
  <input class="btn btn-block btn-success btn-sm fileload upb" onclick="event.preventDefault()"  ontype="event.preventDefault()" type="submit" name="submit" value=" Загрузить ">
</form>


А вот код самого index.php:
<?php


require_once('functions.php');
?>
<link rel="stylesheet" href="images/styles.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="images/scripts.js"></script>
<link rel="stylesheet" href="images/tabs.css">
<script type="text/javascript" src="images/tabs.js"></script>
<script>
			var modalWindow = {
				_block: null,
				_win: null,
		
				initBlock: function() {
					_block = document.getElementById('blockscreen'); 

					
					if (!_block) {
						var parent = document.getElementsByTagName('body')[0]; 
						var obj = parent.firstChild; 
						_block = document.createElement('div'); 
						_block.id = 'blockscreen';
						parent.insertBefore(_block, obj); 
						_block.onclick = function() { modalWindow.close(); } 
					}
					_block.style.display = 'inline';     
				},

				initWin: function(width, html) {
					_win = document.getElementById('modalwindow'); 
					
					if (!_win) {
						var parent = document.getElementsByTagName('body')[0];
						var obj = parent.firstChild;
						_win = document.createElement('div');
						_win.id = 'modalwindow';
						parent.insertBefore(_win, obj);
					}
					_win.style.width = width + 'px'; 
					_win.style.display = 'inline'; 
				
					_win.innerHTML = html; 
				
					

					_win.style.left = '50%';
					_win.style.top = '50%'; 

					
					_win.style.marginTop = -(_win.offsetHeight / 2) + 'px'; 
					_win.style.marginLeft = -(width / 2) + 'px';
				},

				close: function() {
					document.getElementById('blockscreen').style.display = 'none';
					document.getElementById('modalwindow').style.display = 'none';        
				},

				show: function(width, html) {
					modalWindow.initBlock();
					modalWindow.initWin(width, html);
				}
			}
			<?php if(isset($mmesage)) { ?>
			window.onload = function(){
			modalWindow.show(300, '<?=$mmesage ?>');
			}
			<?php } ?>
function Ftest (obj)
{
if (this.ST) return; var ov = obj.value;
var ovrl = ov.replace (/\-?\d*\.?\d*/, '').length; this.ST = true;
if (ovrl > 0) {obj.value = obj.lang; Fshowerror (obj); return}
obj.lang = obj.value; this.ST = null;
}
 
function Fshowerror (obj)
{
if (!this.OBJ)
   {this.OBJ = obj; obj.style.backgroundColor = 'pink'; this.TIM = setTimeout (Fshowerror, 50)}
else
   {this.OBJ.style.backgroundColor = ''; clearTimeout (this.TIM); this.ST = null; Ftest (this.OBJ); this.OBJ = null}
}
</script>


И вот то, что в functions.php
case 'skinfile': 
	$iwh = GetImageSize($_FILES['skinfile']['tmp_name']);
	if ($_FILES["skinfile"]["type"] != "image/png") $result = 'Недопустимый тип файла!';
	elseif(!is_uploaded_file($_FILES["skinfile"]["tmp_name"])) $result = 'Ошибка при загрузке файла!';
	elseif($iwh[0]>$skin_max_size_W || $iwh[1]>$skin_max_size_H || $iwh[0]<$skin_min_size_W || $iwh[1]<$skin_min_size_H) $result = 'Недопустимое разрешение файла!';
	elseif(move_uploaded_file($_FILES["skinfile"]["tmp_name"], 'uploads/skins/'.$username.'.png')) $result = 'Скин загружен!';
	else $result = 'Ошибка при загрузке файла!';
	$mmesage = $mmesag1e.$result.$mmesag2e;
	break;

Думаю, этого вам будет достаточно.
  • Вопрос задан
  • 3566 просмотров
Пригласить эксперта
Ответы на вопрос 1
Sanasol
@Sanasol Куратор тега JavaScript
нельзя просто так взять и загуглить ошибку
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы