[英]How to send a stl file from js using ajax to php without form data
用戶以js導入.stl文件,該文件必須保存到數據庫或服務器文件夾中。 使用javascript我們正在從js導入文件,但是由於我們無法將其保存在服務器中,因此我使用ajax將文件從js發送到php。 這樣就可以從php將其保存在我們的文件夾中。
下面是獲取文件的代碼,但是該文件不會發送到php。 請幫助我如何將js文件從js發送到php
Stage.prototype.handleFile = function(file) { this.importingMeshName = file.name; this.importEnabled = false; var model = new Model( this.scene, this.camera, this.container, this.printout, this.infoBox, this.progressBarContainer ); model.isLittleEndian = this.isLittleEndian; model.vertexPrecision = this.vertexPrecision; model.import(file, this.displayMesh.bind(this)); var form_data = new FormData(); form_data.append("file", document.getElementById('file').files[0]); jQuery.ajax ({ url: 'saveinjs.php', type: "POST", data: {user:1, postdata:form_data}, contentType: false, cache: false, processData: false, success:function(data) { $('#uploaded_image').html(data); alert(data); console.log(data); }, error: function(xhr, textStatus, error){ console.log(xhr.statusText); console.log(textStatus); console.log(error); } }); };
這是php代碼,用於提取文件並保存在我們的文件夾中
<?php if(isset($_POST['user'])){ echo '123'; if($_POST["file"]["name"] != '') { $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) ; } } ?>
您可以使用javascript File
api讀取base64格式的內容。 然后,您可以在后端代碼中解密以獲取實際內容。 以下是用於處理文件的前端代碼
function handleFile(file) { var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function () { console.log(reader.result); var data = {data: reader.result}; // You can post data object to server in json format // Make Ajax request }; reader.onerror = function (error) { console.log('Error: ', error); // Handle error }; } var file = document.getElementById('file').files[0]; handleFile(file);
並在php中使用base64_decode
函數,您可以解碼base64字符串, http: base64_decode
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.