这样也可:
http://dev.csdn.net/develop/article/20/20937.shtm
http://dev.csdn.net/develop/article/20/20937.shtm
解决方案 »
- 翻页时传过来的参数丢失
- 连接远程桌面,输入密码总是提示密码不正确
- 为什么我本地调试VS2010 .net 项目,登陆进页面后的地址不是本机的地址Localhost,而是发布出去的版本
- 关于存储过程的一个简单select语句
- button如何先执行客户端,再执行服务器
- ASP.net的gridview使用显示数据时如何获取某行字段值
- 内联代码中"<script>"的问题
- 问一个 网站浏览权限分级 的问题,web.config 的设置问题 !!!!!
- Entity framework如何动态排序?传入不同的字段进行排序
- 大家有这种情况的进来看看
- 求助:怎样在WebForm中取得鼠标的坐标? 用VB~!谢谢先!
- ASP。NET如何存取图片。
需要C#的,那个代码我看过,VB的,而且太繁琐,我现在只要检测图片是否重复。
<%--
upload.aspx
这是一个上传文件,从中我们可以看出asp.net的优势,以前需要控件才能完成的程序,现在只需要几行代码就可以轻松完成。
--%>
<%@ Import Namespace="System.IO" %>
<%@ page Language="C#" debug="true" codepage="936"%> <html>
<head>
<title>文件上传,aspcool.com </title>
<script language="C#" runat="server"> public void UploadFile(object sender , EventArgs E)
{ if(myFile.PostedFile!=null)
{
//定义一些变量
string nam = myFile.PostedFile.FileName ;
int i= nam.LastIndexOf("\\") ;
string newnm =nam.Substring(i) ;
//取得上传文件的各种属性。
fname.Text=myFile.PostedFile.FileName;
//取得文件名,然后用File.Exists 方法确定指定的文件是否存在
//存在则换名上传.fenc.Text=myFile.PostedFile.ContentType ;
fsize.Text=myFile.PostedFile.ContentLength.ToString(); //改变下面的“c:\\"到你想要保存的地址。
myFile.PostedFile.SaveAs("c:\\"+newnm) ;
}
} </script>
</head>
<body>
<center>
<h3> 文件上传演示</h3>
<form id="uploderform" method="post" action="upload.aspx" enctype="multipart/form-data" runat="server" > <table border="1" cellspacing="0" cellpadding="0" >
<tr> <td><h5>选择文件</h5></td</tr>
<tr><td>
<input type="file" id="myFile" runat="server" >
</td></tr>
<tr><td>
<input type="button" value="Upload" OnServerClick="UploadFile" runat="server" >
</td></tr>
</table>
</form>
<br>
<br>
<table border="1" cellspacing="0">
<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>
<h5>你可以从 <a href="http://www.aspcool.com/download">http://www.aspcool.com/download</a> 下载该程序.我们将给您提供更多的其它程序。如果有什么意见请到我的<a href="http://www.chaxiu.com/club/right.php?boardid=7&page=1">ASP论坛</a>来讨论。</h5>
</center>
</body>
</html>
如果不需要保留上传的原始文件名可以在上传的时候用系统时间作为文件名,根据站点的访问情况,一般精确到毫秒就能够保证文件不会重名了;
2,
如果文件名是让用户自定义的,一种就是在上传的时候用File.Exists检测文件是否存在,如果已经存在的话提示用户更改名称,另外一种方法就是在数据库中增加一个字段,用来保存用户自定义的名称,而实际文件名是根据系统时间确定的
//该文件已经存在;
else
//上传文件
DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+lbKcId.Text+"."+strFl;
wkFl.PostedFile.SaveAs(Server.MapPath("work\\"+strFl));
的方法是可行的
//该文件已经存在;
else
//上传文件
GOOD!
{
HttpContext.Current.Response.Write( "Warning:<br>You had uploaded the same name attachment today.<br>Please go <a href='#' onclick='javascript:history.back();'>Back</a> to rename your attachment and retry!");
HttpContext.Current.Response.End();
return null;
}
postedFile.SaveAs(filePath);
if(File.Exists(DestPath+images_Name))
{
images_Message.Text="图片服务器上已经存在,请换名称";
return;
}
在表里面保存文件路径信息就可以了。
我来给你找点代码:
int ObjectID = int.Parse(Request["ObjectID"].Trim());
string error="";
//获得ID
DocumentData DocumentData1= new DocumentData();
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
int FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength == 0)
{
//文件长度为零时
error = "文件不能为空!";
}
else
{
string[] filenames = UpFile.FileName.Split('\\');
DocumentData1.DocumentName = filenames[filenames.Length-1];
DocumentData1.UserID = Int32.Parse(Session["UserID"].ToString().Trim());
DocumentData1.ObjectID = ObjectID;
DocumentData1.DocumentDespict = txtDocumentDepict.Text.Trim();
DocumentData1.SubmitTime = System.DateTime.Now.ToString();
DocumentData1.DocumentType = "工作文档";
Guid a = Guid.NewGuid();
DocumentData1.FileName = a.ToString().Trim()+".Job";
try
{
UpFile.SaveAs(Request.MapPath("../Data")+@"\"+DocumentData1.FileName.Trim());
}
catch
{
return;
}
Documents.AddDocument(DocumentData1);
//绑定表格数据
DataGridBond();
}
}
catch (Exception ex)
{
error=ex.Message.ToString() ;
}
if ( error != "" )
Response.Write("<script>alert('"+error+"')</script>");
Response.Clear();
SqlDataReader SqlReader = Documents.GetDocumentData(int.Parse(dgDocument.DataKeys[dgDocument.SelectedIndex].ToString().Trim()));
SqlReader.Read();
String FileName = SqlReader["DocumentName"].ToString().Trim();
String FileGuidName = SqlReader["FileName"].ToString().Trim();
//byte[] FileContent = null;
//if (FileGuidName == String.Empty)
FileContent = (byte[])SqlReader["DocumentContent"];
String FilePath = Request.MapPath("../Data")+@"\"+FileGuidName;
SqlReader.Close();
if (FileName.Length > 20)
FileName = FileName.Substring(0,16) + FileName.Substring(FileName.Length-4);
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "Application/Octet-Stream";
Response.AppendHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlEncode(FileName)+";");
try
{
//if (FileGuidName == String.Empty)
//Response.OutputStream.Write(FileContent, 0, FileContent.Length);
//else
Response.WriteFile(FilePath);
}
catch
{
Response.Redirect("../Common/Message.aspx?Msg=无法调阅该文件,可能已经被物理删除,建议删除该条记录并重新上传文件。", true);
}
finally
{
Response.End();
}