我喜欢尝试通过 javascript XMLHttpRequest 发送带有附件的 POST 表单。
这是我的代码:
window.addEventListener('load', function() {
var button = document.getElementById('start_file_upload_button');
button.addEventListener('click', function()
{
document.getElementById("file_upload_button_button").style.display = "none";
var mfs = document.getElementsByName('MAX_FILE_SIZE')[0].value,
to_up_files = document.getElementById('to_upload_files_field'),
status = document.getElementById('file_upload_status_text'),
errorlog = document.getElementById('file_upload_error_log'),
progress = document.getElementById('file_upload_progress_bar');
for(var i = 0; i < document.getElementById('to_upload_files_field').files.length; ++i) {
document.getElementById('remove_link_' + i).style.display = "none"; }
for(var i = 0; i < <?php echo $__CONFIG['UPLOAD']['MAX_FILE_AT_ONCE']; ?>; ++i)
{
if(typeof(document.getElementById('to_upload_files_field').files[i]) == "undefined") break;
var upload = document.getElementById('to_upload_files_field').files[i],
filename = upload.name,
filesize = upload.size,
mfs = mfs; alert(filename);
if(filesize > <?php echo $USER_DATA['UPLOAD_MAX_FILE_SIZE']; ?>)
{
var errorelem = document.createElement('span');
var errorbreak = document.createElement('br');
errorelem.appendChild(document.createTextNode('Die Datei "' + filename + '" ist zu Groß'));
errorelem.appendChild(errorbreak);
errorlog.appendChild(errorelem);
break;
}
if(<?php echo $USER_DATA['SPACE_MAX']; ?> < (filesize + <?php echo $USER_DATA['SPACE_USED']; ?>))
{
var errorelem = document.createElement('span');
var errorbreak = document.createElement('br');
errorelem.appendChild(document.createTextNode('Die Datei "' + filename + '" past nicht mehr auf die Platte'));
errorelem.appendChild(errorbreak);
errorlog.appendChild(errorelem);
break;
}
if(filesize > (<?php echo $USER_DATA['TRAFFIC_PER_DAY'] - $USER_DATA['TRAFFIC_USED']; ?>) && (<?php echo $USER_DATA['TRAFFIC_PER_DAY'] - $USER_DATA['TRAFFIC_USED']; ?>) != 0)
{ // SPERRE EINBAUEN DAS ES NUR GEPRÜFT WIRD WENN DIE TRAFFIC ÜBERWACHUNG AKTIVIERT IST
var errorelem = document.createElement('span');
var errorbreak = document.createElement('br');
errorelem.appendChild(document.createTextNode('Die Datei "' + filename + '" braucht mehr traffic als du hast'));
errorelem.appendChild(errorbreak);
errorlog.appendChild(errorelem);
break;
}
status.innerHTML = "Lade " + filename + " hoch...";
var request = new XMLHttpRequest();
request.open('POST', 'http://www.domain.tld/user/upload/', true);
request.setRequestHeader('Content-Type', 'multipart/form-data');
request.upload.addEventListener('progress', function(evt)
{
var uploaded = Number((100 / evt.total) * evt.loaded).toFixed(2);
progress.innerHTML = "Upload zu " + uploaded + "% fertig";
if(uploaded = 100.00)
{
progress.innerHTML = "Datei wird verarbeitet... Bitte warten...";
}
}, false);
request.addEventListener('load', function(evt) {
alert("lol2"); }, false);
var data = new FormData();
data.append('upload_key', '<?php echo $upload_key; ?>');
data.append('MAX_FILE_SIZE', mfs);
data.append('file', upload);
request.send(data);
request.onload = function()
{
if(this.status == 200)
{
alert("lol");
}
else
{
alert("Some Upload Error");
}
};
}
}, false);
}, false);
当我将脚本发送到“ http://www.domain.tld/user/upload/ ”时,该脚本运行良好且没有问题,但我喜欢将其发送到“ http://sub.somain.com/store/ ” “这不起作用,为什么?
通过发送到“www.domain.tld”的控制台日志:
[12:21:51.085] POST
http://www.domain.tld/user/upload/
[HTTP/1.1 200 OK 5ms]
通过发送到“sub.domain.tld”的控制台日志:
[12:21:17.786] 选项
http://sub.domain.tld/store/
[HTTP/1.1 200 OK 7ms]
服务器运行相同的脚本,我该怎么办?