是的,Android WebView支持文件上传。要在WebView中实现文件上传功能,您需要执行以下步骤:
<input type="file">
)和一个提交按钮(<input type="submit">
)。<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="your_upload_url" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>
</body>
</html>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setBuiltInZoomControls(false);
webSettings.setSupportZoom(false);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file:///android_asset/file_upload_form.html");
}
}
请注意,您需要将your_upload_url
替换为您的服务器端文件上传处理URL。
<?php
if (isset($_FILES['fileToUpload'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// 检查文件是否为有效上传
if (isset($_POST["submit"])) {
$uploadOk = 1;
} else {
echo "File was not uploaded.";
$uploadOk = 0;
}
// 检查上传文件夹是否存在
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
// 尝试上传文件
if ($uploadOk == 1) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars(basename($_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
} else {
echo "No file was uploaded.";
}
?>
确保您的服务器端代码已正确配置,以便处理文件上传。