js如何实现本地图片路径拖放至网页上传?
你说的是自动输入某些代码到浏览器console?
既然都是自动化,不如用selenium,相当于可模拟鼠标键盘操作。
如何使用 NodeJS 将文件或图像上传到服务器
下面先介绍上传文件到服务器(多文件上传):
import .*;
import .*;
import .*;
import .*;
import .*;
import .*;
public class upload extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
(CONTENT_TYPE);
PrintWriter out=();
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置允许用户上传文件大小,单位:字节,这里设为2m
(2*1024*1024);
// 设置最多只允许在内存中存储的数据,单位:字节
(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
("c://windows//temp");
//开始读取上传信息
List fileItems = (request);
// 依次处理每个上传的文件
Iterator iter = ();
//正则匹配,过滤路径取文件名
String regExp=".+////(.+)$";
//过滤掉的文件类型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = (regExp);
while (()) {
FileItem item = (FileItem)();
//忽略其他不是文件域的所有表单信息
if (!()) {
String name = ();
long size = ();
if((name==null||("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;temp<;TEMP++){
if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{
//保存上传的文件到指定的目录
//在下文中上传文件至数据库时,将对这里改写
(new File("d://" + m.group(1)));
(name+" "+size+"");
}
catch(Exception e){
(e);
}
}
else
{
throw new IOException("fail to upload");
}
}
}
}
catch (IOException e){
(e);
}
catch (FileUploadException e){
(e);
}
}
}
现在介绍上传文件到服务器,下面只写出相关代码:
以sql2000为例,表结构如下:
字段名:name filecode
类型: varchar image
数据库插入代码为:PreparedStatement pstmt=("insert into test values(?,?)");
代码如下:
。。。。。。
try{
这段代码如果不去掉,将一同写入到服务器中
//(new File("d://" + m.group(1)));
int byteread=0;
//读取输入流,也就是上传的文件内容
InputStream inStream=();
(1,m.group(1));
(2,inStream,(int)size);
();
();
(name+" "+size+" ");
}
。。。。。。
这样就实现了上传文件至数据库
怎样用js或者jq实现点击这个图片就可以选择上传还有预览图片啊
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script src="jquery-3.1.1."></script>
</head>
<body>
<h3>请选择图片文件:JPG/GIF</h3>
<form name="form0" id="form0" >
<input type="file" name="file0" id="file0" multiple="multiple" />
<br><br><img src="" id="img0" width="120">
</form>
</body>
<script>
$("#file0").change(function(){
var objUrl = getObjectURL([0]) ;
("objUrl = "+objUrl) ;
if (objUrl)
{
$("#img0").attr("src", objUrl);
$("#img0").removeClass("hide");
}
}) ;
//建立一个可存取到该file的url
function getObjectURL(file)
{
var url = null ;
if (!=undefined)
{ // basic
url = (file) ;
}
else if (!=undefined)
{
// mozilla(firefox)
url = (file) ;
}
else if (!=undefined) {
// webkit or chrome
url = (file) ;
}
return url ;
}
$('input').on('change',function(){
var value = $(this).val();
value = ("\\")[2];
alert(value);
})
</script>
</html>