如题,我只知道用File类实现,具体要什么代码,调用什么函数,就全不知道了,⊙﹏⊙b汗!
Microsoft Visual Studio 2008
ASP.NET3.5的Visual C#
Microsoft Visual Studio 2008
ASP.NET3.5的Visual C#
解决方案 »
- 怎么写热备软件
- 学习注册表
- 一个语句写法 C#
- 紧急求助,为什么获取不行!
- 开发客户端的程序,需要注意那几个方面的问题?请做过大虾给于支持.谢谢.
- 自定义控件无法刷新的问题
- 在C#中,如何获得Datagrid当前列某一栏位的值.(与VB.NET不一样)
- 如在c#做c/s应用程序时定义一个全局的变量(在整个工程都可以使用),哪位老大可以告诉我呀,
- Visual Studio C#--> 如何在物理层创建一个新表?
- 新手请教
- 【高分求解】dataGridView如何分页?如何实现增删改然后保存在SQL数据库?
- 关于跨线程调用控件new thread 不出错,delegate.BeginInvoke就报错,奇怪了
<%@ Import Namespace="System.IO" %>
<%@ page Language="C#" debug="true" %>
<html>
<head>
<title>上传文件 , http://www.chinabs.net </title>
<script language="C#" runat="server">
//This method is called when the "upload" button id pressed
public void UploadFile(object sender , EventArgs E)
{
//检查上传文件不为空
if(myFile.PostedFile!=null)
{
string nam = myFile.PostedFile.FileName ;
//取得文件名(抱括路径)里最后一个"."的索引
int i= nam.LastIndexOf(".");
//取得文件扩展名
string newext =nam.Substring(i);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime now = DateTime.Now;
string newname=now.DayOfYear.ToString()+myFile.PostedFile.ContentLength.ToString();
//保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
myFile.PostedFile.SaveAs(Server.MapPath("\\upload\\"+newname+newext));
//得到这个文件的相关属性:文件名,文件类型,文件大小
fname.Text=myFile.PostedFile.FileName;
fenc.Text=myFile.PostedFile.ContentType ;
fsize.Text=myFile.PostedFile.ContentLength.ToString();
}
}
</script>
</head>
<body>
<center>
<h3> 文件上传的实例, 来自<a href="http://www.chinabs.net">中国BS网</a></h3>
<form id="uploderform" method="post" action="FileUpload.aspx" enctype="multipart/form-data" runat="server" >
<table border="1" cellspacing="2" cellpadding="2" >
<tr> <td><h5>选择要上传的文件:</h5></td</tr>
<tr>
<td>
<input type="file" id="myFile" runat="server" NAME="myFile">
</td>
</tr>
<tr><td>
<input type="button" value="上 传" OnServerClick="UploadFile" runat="server" ID="Button1" NAME="Button1">
</td></tr>
</table>
</form>
<br>
<br>
<table border="1" cellspacing="2">
<tr><td><b>文件资料</b></td>
<td> </td>
</tr>
<tr>
<td>文件名 :</td>
<td><asp:label id="fname" text="" runat="server" /></td></tr>
<tr>
<td>文件类型 :</td>
<td><asp:label id="fenc" runat="server" /></td></tr>
<tr>
<td>文件大小 :(in bytes)</td>
<td><asp:label id="fsize" runat="server" /></td></tr>
</table>
<br>
<br>
<br>
</center>
</body>
</html>
string ConStr = "server=(local);user id=sa ;pwd=123 ;database=qikan";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
string sqlinsert = "insert into 文章(文章名称,文章路径)values('" + newname+ "','"?"')";
SqlCommand com = new SqlCommand(sqlinsert, con);
com.ExecuteNonQuery();
Response.Write("<script language = javascript>alert ('上传成功!')</script>");
con.Close();
除了在开头加上<%@ import Namespace=" System.Data.SqlClient" %>
还有哪些部分要改的,以及这段代码写在那个位置,折腾了一个通宵,没弄出来...
//上传文件
protected void Upload()
{
//判断文件是否选择
if (FileUpload1.HasFile)
{
string filename = FileUpload1.FileName;
filename = "***" + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + filename.Substring(filename.LastIndexOf(".")); //生成服务器端文件名
FilePath = Server.MapPath(@"~\UploadFileTemp\" + filename); //保存文件路径
string fileExt = System.IO.Path.GetExtension(filename);
//判断文件格式
if (fileExt == ".xls" || fileExt == ".xlsx")
{
FileUpload1.PostedFile.SaveAs(FilePath);
......
//将文件名(filename)及路径(FilePath)保存到数据库并获取执行结果 判断是否执行成功
}
else
{
//提示上传格式错误
}
}
}
else
{
//提示选择文件
}
}
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />//将附件传送到服务器上
string path = FileUpload1.PostedFile.FileName.ToString(); //获取上传图片路径
string ext = path.Substring(path.LastIndexOf(".") + 1); //获取图片扩展名
if (ext.ToLower() == "jpg" || ext.ToLower() == "bmp" || ext.ToLower() == "gif" || ext.ToLower() == "png" || ext.ToLower() == "tif" || ext.ToLower() == "jpeg")
{
string sPath = Server.MapPath("upload/" + path); //设置图片保存到服务器上路径 FileUpload1.PostedFile.SaveAs(sPath); //保存到服务器
string ConStr = "server=(local);user id=sa ;pwd=123 ;database=qikan";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
string sqlinsert = "insert into 文章(文章名称,保存路径)values('" + myx1 + "','" + sPath + "')";
SqlCommand com = new SqlCommand(sqlinsert, con);
com.ExecuteNonQuery();
con.Close();
this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('上传成功!');</script>"); }
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('只能上传图片!');</script>");
}F:\RealElec\upload 就是项目下建个文件夹
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class Default11 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string path = FileUpload1.PostedFile.FileName.ToString(); //获取上传图片路径
string ext = path.Substring(path.LastIndexOf(".") + 1); //获取图片扩展名
string name = System.IO.Path.GetFileNameWithoutExtension(path);// 没有扩展名的文件名
if (ext.ToLower() == "jpg" || ext.ToLower() == "bmp" || ext.ToLower() == "gif" || ext.ToLower() == "png" || ext.ToLower() == "tif" || ext.ToLower() == "jpeg")
{
int i = path.LastIndexOf(".");
//取得文件扩展名
string newext = path.Substring(i);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime now = DateTime.Now;
string newname = now.DayOfYear.ToString() + FileUpload1.PostedFile.ContentLength.ToString();
string sPath = Server.MapPath(@"\webmag/UpFiles/\" + newname+newext); //设置图片保存到服务器上路径
string fname = FileUpload1.PostedFile.FileName;
FileUpload1.PostedFile.SaveAs(sPath); //保存到服务器
string ConStr = "server=(local);user id=sa ;pwd=123 ;database=qikan";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
string sqlinsert = "insert into 文章(题目,保存路径)values('" + name + "','" + sPath + "')";
SqlCommand com = new SqlCommand(sqlinsert, con);
com.ExecuteNonQuery();
con.Close();
this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('上传成功!');</script>"); }
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('只能上传图片!');</script>");
} }
}
html码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>