求教WEB上传图片 第一次写WEBFORM,请教1、如何打开类似一个opendialog的东西;2、数据如何传输到数据库。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、如何打开类似一个opendialog的东西; 你可以直接使用html 控件 input(file)2 数据 传到 数据库 没有什么特别的 和 传统的 步骤 一样 asp.net里有上传控件,直接用就是了。 拖一个叫FileUpload的控件.(vs工具箱里就有.)然后参考下面代码bool fileOK = false; string path = Server.MapPath("~/UploadedImages/"); if (FileUpload1.HasFile) { string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); string[] allowedExtensions = {".gif", ".png", ".jpeg", ".jpg"}; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } if (fileOK) { try { FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName); Label1.Text = "File uploaded!"; } catch (Exception ex) { Label1.Text = "File could not be uploaded."; } } else { Label1.Text = "Cannot accept files of this type."; } 两种方法: 一种是楼上的 用html的input(file)控件还有一种 可以用asp.net的相关控件 asp:FileUpload然后在 提交表单的处理事件中(例如Button点击事件)中如下处理:string msg = ""; try { if (FileUpload1.PostedFile.FileName == "") { msg = "请选择文件! "; } else { //这里主要是处理文件名称 类型之类的 string filepath = FileUpload1.PostedFile.FileName; string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1); //这里设置文件的保存路径 string serverpath = Server.MapPath("upload/")+filename; //上传(保存文件) FileUpload1.PostedFile.SaveAs(serverpath); msg = "上传成功! "; } } catch (Exception ex) { msg = "上传发生错误!原因是: " + ex.ToString(); }Response.Write(msg);至于数据...看你自己想保存文件名还是完整路径 直接赋值 写入数据库就OK~!O(∩_∩)O~ 我是不想拖那个控件出来,那个控件很难看,可以借助其他办法吗?刚才上网找了下用script可以实现,一时手快关掉了没留意 1:在JSP界面中写:<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="com.jspsmart.upload.*" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body leftmargin="0" topmargin="0" bgcolor="#ffffff"><form action="ss.jsp" method="post" name="form1" enctype="multipart/form-data"> <input name="FileName" type="FILE" size="30"> <input type="submit" name="Submit" value="上传" > </form></body></html><%@ page contentType="text/html;charset=gb2312"%> <%@ page import="com.jspsmart.upload.*" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body leftmargin="0" topmargin="0" bgcolor="#ffffff"><form action="ss.jsp" method="post" name="form1" enctype="multipart/form-data"> <input name="FileName" type="FILE" size="30"> <input type="submit" name="Submit" value="上传" > </form></body></html>2:在SS.jsp中写:<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ page import="com.jspsmart.upload.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030"><title>Insert title here</title></head><%SmartUpload myupload=new SmartUpload();myupload.initialize(pageContext);myupload.upload();File file=myupload.getFiles().getFile(0);String filename=file.getFileName();file.saveAs("/file/"+filename,myupload.SAVE_VIRTUAL);out.print("文件上传成功!"); %><body></body></html>注意:要引入smartupload.jar包。 调试已经发布的web程序 如何实现鼠标左键单击跳出菜单 静态函数的问题 关于c#中MDI窗体设计的问题!请教各位大虾! C# 弹出文件对话框 这个问题如何解决 求助,关于正则表达式 向文本文件中输入回车的问题 C#问题,新手求助 移动adc和si接口对接 开启和关闭系统服务 C#的一个很简单的问题。。请高手帮忙。小弟无尽感激
string path = Server.MapPath("~/UploadedImages/");
if (FileUpload1.HasFile)
{
string fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
string[] allowedExtensions =
{".gif", ".png", ".jpeg", ".jpg"};
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
} if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path
+ FileUpload1.FileName);
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
还有一种 可以用asp.net的相关控件 asp:FileUpload
然后在 提交表单的处理事件中(例如Button点击事件)中如下处理:
string msg = "";
try
{
if (FileUpload1.PostedFile.FileName == "")
{
msg = "请选择文件! ";
}
else
{
//这里主要是处理文件名称 类型之类的
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
//这里设置文件的保存路径
string serverpath = Server.MapPath("upload/")+filename;
//上传(保存文件)
FileUpload1.PostedFile.SaveAs(serverpath);
msg = "上传成功! ";
}
}
catch (Exception ex)
{
msg = "上传发生错误!原因是: " + ex.ToString();
}
Response.Write(msg);至于数据...看你自己想保存文件名还是完整路径 直接赋值 写入数据库就OK~!
O(∩_∩)O~
用script可以实现,一时手快关掉了没留意
<%@ page import="com.jspsmart.upload.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body leftmargin="0" topmargin="0" bgcolor="#ffffff">
<form action="ss.jsp" method="post" name="form1" enctype="multipart/form-data">
<input name="FileName" type="FILE" size="30">
<input type="submit" name="Submit" value="上传" >
</form>
</body>
</html><%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.jspsmart.upload.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body leftmargin="0" topmargin="0" bgcolor="#ffffff">
<form action="ss.jsp" method="post" name="form1" enctype="multipart/form-data">
<input name="FileName" type="FILE" size="30">
<input type="submit" name="Submit" value="上传" >
</form>
</body>
</html>2:在SS.jsp中写:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%> <%@ page import="com.jspsmart.upload.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<%SmartUpload myupload=new SmartUpload();
myupload.initialize(pageContext);
myupload.upload();
File file=myupload.getFiles().getFile(0);
String filename=file.getFileName();
file.saveAs("/file/"+filename,myupload.SAVE_VIRTUAL);
out.print("文件上传成功!");
%>
<body></body>
</html>
注意:要引入smartupload.jar包。