我已经实现了这个脚本用于上传带有ajax的文件,它在浏览器以外的其他浏览器中工作得很完美,我注意到IE9不支持formData,更少,IE中的formData有什么替代品,我想用干净的javascript
function doObjUploadExplorer(url, lnk_id, file, progress, success, content, frm, div_dlg, start_func){
var file_input = null,
frm_data = new FormData(),
req;
try {
//firefox, chrome, safari etc
req = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer Browsers
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (document.getElementById(file)) {
file_input = document.getElementById(file);
for (var i = 0; i < file_input.files.length; ++i) {
frm_data.append(file, file_input.files[i]);
}
}
req.upload.addEventListener('progress', function(e) { //Event called while upload is in progress
if (progress !== undefined
&& e.lengthComputable) { …Run Code Online (Sandbox Code Playgroud) 我有一个表单,通过ajax向服务器提交值.
<form>
<input id="titlee" name="titlee">
<input type="file" name="fileToUpload" id="fileToUpload">
<button type="submit" value="submit" id="submit" name="submit">Start</button>
<div class="progress"></div>
</form>
Run Code Online (Sandbox Code Playgroud)
脚本
<script type="text/javascript">
$(function()
{
$("#submit").click(function()
{
var titlee = $("#titlee").val();
var fileToUpload= $("#fileToUpload").val();
var dataString = 'titlee='+ titlee + '&fileToUpload=' + fileToUpload;
$.ajax(
{
type: "POST",
url: "c_insert_test.php",
data: dataString,
success: function()
});
return false;
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
c_insert_test.php
<?php
$titlee = $_POST['titlee'];
$target_dir = "reqdoc/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$new_filename = $target_dir . uniqid() …Run Code Online (Sandbox Code Playgroud) 我有一个现有的html表单,一旦用户选择了一个图像文件,就会将文件上传到服务器.
我做过这样的事情.
//html code
<input type="file" id="photo" name="photo" accept="image/*" />
// the usual html stuff
document.getElementById('photo').addEventListener("change",uploadImage);
function uploadImage()
{
var xhr = new XMLHttpRequest();
xhr.open("POST","/upload.php",true);
xhr.setRequestHeader("Content-type","image");
var file = document.getElementById('photo').files[0];
if(file)
{
var formdata = new FormData();
formdata.append("pic",file);
xhr.send(formdata);
}
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200)
{
//some code
}
};
}
Run Code Online (Sandbox Code Playgroud)
但在我的php文件中,我无法访问此上传的文件.该$_POST数组似乎是空的.我做了一个print_rfor $_POST,它给了Array().我究竟做错了什么?