1) There are many methods out there... for example:
1)有很多方法......例如:
http://valums.com/ajax-upload/
http://valums.com/ajax-upload/
http://www.webtoolkit.info/ajax-file-upload.html
http://www.webtoolkit.info/ajax-file-upload.html
(and many more. Just google for: "Ajax file upload" or "iframe file upload")
(以及更多。仅谷歌:“Ajax文件上传”或“iframe文件上传”)
2) Don't worry about the width of the field. As you don't know how long can it be the path, it would never be long enough (i think). Also browsers may display it very different. For example Safari or Chrome show it very different from Firefox or IE. Just use the default length or the one that looks better with your design.
If you submit the form onchange, you won't notice the field get populated and therefore should not need to change the text length. If you desire to, I would get the character count of the string in the field and then apply a size attribute to the input
In case you don't want or need to submit the whole form on file input select, then you can do this to send only the file:
如果您不想或不需要在文件输入选择上提交整个表单,那么您可以这样做只发送文件:
$(document).ready(function (e) {
$("#uploadImage").on('change',(function(e) {
// append file input to form data
var fileInput = document.getElementById('uploadImage');
var file = fileInput.files[0];
var formData = new FormData();
formData.append('uploadImage', file);
$.ajax({
url: "ajaxupload.php",
type: "POST",
data: formData,
contentType: false,
cache: false,
processData:false,
success: function(data) {
if(data=='invalid') {
// invalid file format.
$("#err").html("Invalid File !").fadeIn();
}
else {
// view uploaded file.
$("#preview").html(data).fadeIn();
$("#form")[0].reset();
}
},
error: function(e) {
$("#err").html(e).fadeIn();
}
});
}));
});
So first you get the input file element with getElementById(), then you append to FormData, then send the FormData as data in your ajax call. Do the server side upload processing in ajaxupload.php.