[英]How to include enctype="multipart/form-data" if I am not using <form> tag to upload file to database?
I want to upload pdf file to my database.我想将pdf文件上传到我的数据库。 On skimming through various resources I found that we have to include attribute enctype="multipart/form-data" in form tag.
在浏览各种资源时,我发现我们必须在表单标签中包含属性 enctype="multipart/form-data"。 Problem is am not using form to fill data (coz of CSS designing and framework).
问题是我没有使用表单来填充数据(因为 CSS 设计和框架)。 form data is submitted by onClick function.
表单数据由 onClick 函数提交。
So how can I still upload file?那么我怎样才能上传文件呢? I think we cant include enctype attribute in tag?
我认为我们不能在标签中包含 enctype 属性?
You can simple do this by this jQuery function你可以通过这个 jQuery 函数简单地做到这一点
function uploadFile(blobFile) {
var fd = new FormData();
fd.append("fileToUpload", blobFile);
$.ajax({
url: "upload.php",
type: "POST",
data: fd,
processData: false,
contentType: false,
success: function(response) {
// .. do something
},
error: function(jqXHR, textStatus, errorMessage) {
console.log(errorMessage); // Optional
}
});
}
Usage用法
For this input对于这个输入
<input type="file" id="filechooser">
Use this用这个
uploadFile($('#filechooser')[0].files[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.