无法将文件从android 4.4上传到webview

时间:2014-04-02 14:22:07

标签: php file-upload cordova webview android-4.4-kitkat

我在谷歌和这里搜索了很多但是真的找不到使用HTTP请求或JavaScript或电话差距在android 4.4上传的方法 我尝试使用POST上传HTTP请求 这是我的代码

public class Upload_File {

private int serverResponseCode;
private String serverResponseMessage;

public void up_myfile(String pathToOurFile , String urlServer) {
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;


    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    serverResponseCode = connection.getResponseCode();
    serverResponseMessage = connection.getResponseMessage();

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        Log.e("MYAPP", "exception", ex);
    }
}

} 在服务器端:

if (isset($_FILES)) {
         if (is_uploaded_file($_FILES['userfile'])) {
             if (0 === $_FILES['userfile']['error']) {
                        echo "there is a file"; 
                    }else{echo "mafeesh file error number ".$_FILES['userfile']['name']."</br>";}   

                }else{echo "there is NO a file<br>";}   

    }

    $filename  = basename($_FILES['userfile']['name']);
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    $new= $id.'.'.$extension;
    echo "test uploading !!! ".$_FILES['userfile']['tmp_name']."</br>";
    if(move_uploaded_file($_FILES["userfile"]["tmp_name"],"photos/" .$new)){
    echo "The file ".  $filename. " has been uploaded";
    } else{
     echo "There was an error uploading the file ??, please try again!";
    }   

$ _ FILES ['userfile'] ['name']为空,没有上传任何内容 帮助我迷路的人:( :(

1 个答案:

答案 0 :(得分:1)

如果是phonegap项目或Application Use phonegap fileTransfer方法,首先你需要将这个插件安装到phonegap项目

cordova plugin add org.apache.cordova.file-transfer



// !! Assumes variable fileURL contains a valid URL to a text file on the device,
//    for example, cdvfile://localhost/persistent/path/to/file.txt

var win = function (r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

var fail = function (error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "text/plain";

var params = {};
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);