我无法解码从JAVASCRIPt传递到PHP的文件

我无法解码从JAVASCRIPt传递到PHP的文件,javascript,php,ajax,Javascript,Php,Ajax,我正在编码一个文件(word文档)并将其传递给php。如何读取和写入文件 我有submit按钮。在submit上,我传递了一个ajax。在此之前,我用文件读取器对文件进行编码。在submit按钮上,一个事件“handleFileSelect”被触发。文件被读取为dataurl,并通过ajax发送到php 我能够得到编码后的数据。如果文件是文本,我也能够解码。但是 它无法获取word文件的内容。如果我解码 我该怎么做 我的代码: //File Convertion--Function to con

我正在编码一个文件(word文档)并将其传递给php。如何读取和写入文件

我有submit按钮。在submit上,我传递了一个ajax。在此之前,我用文件读取器对文件进行编码。在submit按钮上,一个事件“handleFileSelect”被触发。文件被读取为dataurl,并通过ajax发送到php

我能够得到编码后的数据。如果文件是文本,我也能够解码。但是 它无法获取word文件的内容。如果我解码 我该怎么做

我的代码:

//File Convertion--Function to convert images to base 64 encoded format
function handleFileSelect(objEvent) {
  var strFiles = objEvent.target.files; // FileList object
  strInput = document.getElementById('uploaded_file');
  strFile = strInput.files[0];
  strFiletype=strFile.type;
  strFileSize=strFile.size;alert(strFiletype);
  strFiletype=strFiletype.split("/");

  //Checking wheter the uploaded file is image or not
  if(strFiletype[0]!='image') {
    for (var i = 0, f; f = strFiles[i]; i++) { 
      var reader = new FileReader();
      // Closure to capture the file information.
      reader.onload = (function(theFile) {
              return function(e) {
                    // Render thumbnail.
                    strGlobalImageData=e.target.result;
              };
      })(f);
      reader.readAsDataURL(f);
    } 
  } else {
        alert("NOT A DOC");
  }
 }
//ajax call to send files to php
var app = 'contact.php';
$.ajax({
    url: app,
    async: false,
    type:"POST",
    data : "file="+strGlobalImageData,
    dataType: "jsonp",
    contentType: 'application/x-www-form-urlencoded',
    processData:false,
    jsonp: "jsoncallback",
    success: function(html) {
         alert("Thank you. We will be in touch with you");
    },
    error: function(){
      alert("Thank you. We will be in touch with you");
    }
});

//Php side--contact.php
<?php
$files=base64_decode($_POST['file']);
//文件转换--将图像转换为base 64编码格式的函数
功能手柄文件选择(对象){
var strFiles=objEvent.target.files;//文件列表对象
strInput=document.getElementById('uploaded_file');
strFile=strInput.files[0];
strFiletype=strFile.type;
strFileSize=strFile.size;警报(strFiletype);
strFiletype=strFiletype.split(“/”);
//正在检查上载的文件是否为图像
if(strFiletype[0]!='image'){
对于(vari=0,f;f=strFiles[i];i++){
var reader=new FileReader();
//闭包以捕获文件信息。
reader.onload=(函数(文件){
返回函数(e){
//渲染缩略图。
strGlobalImageData=e.target.result;
};
})(f) );
reader.readAsDataURL(f);
} 
}否则{
警报(“非文件”);
}
}
//将文件发送到php的ajax调用
var-app='contact.php';
$.ajax({
url:app,
async:false,
类型:“POST”,
数据:“file=“+strGlobalImageData,
数据类型:“jsonp”,
contentType:'application/x-www-form-urlencoded',
processData:false,
jsonp:“jsoncallback”,
成功:函数(html){
警惕(“谢谢,我们会与您联系”);
},
错误:函数(){
警惕(“谢谢,我们会与您联系”);
}
});
//Php端--contact.Php

问题在于字符替换。我们需要在解码数据之前进行替换。确切代码如下所示: 在Php中

$files=trim($_POST['file'])

$strengcodeddata=str_replace(“,+”,$files)

$strFilteredData=explode(“,”,$strengcodeddata)

$strdecodedata=base64_decode($strfilteredata[1])

$arrFiles=分解(“,”,$files)

文件内容(“myfile.doc”,$srdecodedata)

此内容将写入文件“myfile.doc”


谢谢各位朋友们的努力,你可以考虑编辑你的问题以便于阅读。没有人愿意阅读你的问题。@linn你正在将doc传递给php,或者将word文档中的数据传递给php?嗨,我正在将doc传递给php。但是它将是编码格式的。例如:如果我键入die($_POST['file']),我会得到数据:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UESDBBQabgaiaaaaiqb4pzowweaaouhaataagcw0nvbnrlbnrfvhlwzxndln:):我明白了……我试过我自己的方法,我明白了。我现在在下面回答