[英]Upload image via Ajax
Well the thing is there is already a edited form with a lot of fields but all the save and validate goes trough ajax. 好了,已经有很多字段的已编辑表单,但是所有保存和验证都通过ajax完成。 They asked me now to put a file upload , i tough that just will be set a input and get it on back , but since all goes trough ajax i cant.
他们现在要求我上传文件,我坚决只是将其设置为输入并将其放回原处,但是由于所有操作都无法通过ajax进行。
I don't want to change all the function and go trough a submit if it's not necessary. 我不想更改所有功能,并在不需要时通过提交。 I looked for some uploaders of file trough ajax but all of them are type drag and drop and i don't like them because y only need a simple file.
我寻找了一些文件槽ajax的上传器,但它们都是拖放类型,我不喜欢它们,因为y只需要一个简单的文件。 And the ones that i found that looked simple where in flash...
我发现的那一瞬间看起来很简单...
Is there any simple script that allows me to upload a simple file trough ajax without need of change the type of submitting the fields. 是否有任何简单的脚本可以让我通过ajax上传简单文件,而无需更改提交字段的类型。
Thank's in advance mates ;) 预先感谢队友;)
//the js that saves all the inputs
function _edit_campaign(){
var data = formvalues_inspinia("body");
data.action=_action;
data.status=$("#smf_ior_status").val();
$.ajax({
url: "/save_changes",
dataType: "json",
data: data,
method:"POST",
success: function (response) {
if(!response.status){
toastr_error(response.desc);
$( "#submit_confirm" ).prop( "disabled", false );
$("#"+response.camp).focus();
}else{
toastr_success(response.desc);
}
}
});
} }
client side 客户端
$.ajax({
url: "ajax_php_file.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
});
server side 服务器端
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "upload/".$_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
You can achieve this in simpler way using "ajaxSubmit". 您可以使用“ ajaxSubmit”以更简单的方式实现此目的。 Include jquery.form.js on your page and submit your form.
在页面上包含jquery.form.js并提交表单。
$(form).ajaxSubmit({
url: url,
type: "POST",
success: function (response) {
// do what you need with response
});
It sends all form data including file on server then you can handle these data in regular manner. 它发送所有表格数据,包括服务器上的文件,然后您可以按常规方式处理这些数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.