这样也可:
http://dev.csdn.net/develop/article/20/20937.shtm

解决方案 »

  1.   


    需要C#的,那个代码我看过,VB的,而且太繁琐,我现在只要检测图片是否重复。
      

  2.   

    用asp.net编写的上传程序作者:  发表时间: 2003-7-13 10:12:32 --------------------------------------------------------------------------------
    <%-- 
    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>  
      

  3.   

    1,
    如果不需要保留上传的原始文件名可以在上传的时候用系统时间作为文件名,根据站点的访问情况,一般精确到毫秒就能够保证文件不会重名了;
    2,
    如果文件名是让用户自定义的,一种就是在上传的时候用File.Exists检测文件是否存在,如果已经存在的话提示用户更改名称,另外一种方法就是在数据库中增加一个字段,用来保存用户自定义的名称,而实际文件名是根据系统时间确定的
      

  4.   

    if(File.Exists(文件路径+文件名))
      //该文件已经存在;
    else
      //上传文件
      

  5.   

    用时间明命名你看看strFl=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+
    DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+lbKcId.Text+"."+strFl;
    wkFl.PostedFile.SaveAs(Server.MapPath("work\\"+strFl));
      

  6.   

    zpisgod(GOD) 
    的方法是可行的
      

  7.   

    在数据库中命名一个文件ID的字段,用文件ID+文件名命名就不会重复了
      

  8.   

    if(File.Exists(文件路径+文件名))
      //该文件已经存在;
    else
      //上传文件
      

  9.   

    在数据库中增加一个字段,用来保存用户自定义的名称,而实际文件名是根据系统时间确定的
    GOOD!
      

  10.   

    if(File.Exists(filePath))
    {
    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);
      

  11.   

    自动生成文件名(用时间生成,不会有重的)
    if(File.Exists(DestPath+images_Name))
    {

    images_Message.Text="图片服务器上已经存在,请换名称";
    return;
    }
      

  12.   

    使用guid来命名文件。
    在表里面保存文件路径信息就可以了。
    我来给你找点代码:
    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>");
      

  13.   

    读取的时候可以这样:
    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();
    }