求asp.net图片上传代码,在数据库中存的是图片名,不是地址,存在电脑硬盘中.有合适的发在[email protected]或写在楼下。。

解决方案 »

  1.   

    百度,google一下就一大堆了。  珍惜你的分数啊。
    上传控件下面就直接有一个属性获得图片名的。 还有个属性是获得全路径。
      

  2.   

    protected void btnUpload_Click(object sender, EventArgs e) 
        { 
            if (fileUpload.HasFile) 
            { 
                string savePath = Server.MapPath("~/upload/"); 
                if(!System.IO.Directory.Exists(savePath)) 
                { 
                    System.IO.Directory.CreateDirectory(savePath); 
                } 
                savePath = savePath + "\\" + fileUpload.FileName; 
                fileUpload.SaveAs(savePath);//保存文件 
                 string filename=fileUpload.FileName; 
            } 
        } 
      

  3.   


    <%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Button1_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(FileUpload1.FileName))//判断是否为正确的路径
            { 
                //
                //这里判断是否为图片
                //
                FileUpload1.SaveAs(MapPath("保存路径"));//保存图片
            }
            
        }
    </script><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" Text="上传" OnClick="Button1_Click" /></div>
        </form>
    </body>
    </html>
      

  4.   

    <%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">  protected void UploadButton_Click(object sender, EventArgs e)
      {
        // Specify the path on the server to
        // save the uploaded file to.
        String savePath = @"c:\temp\uploads\";    // Before attempting to perform operations
        // on the file, verify that the FileUpload 
        // control contains a file.
        if (FileUpload1.HasFile)
        {
          // Get the name of the file to upload.
          String fileName = FileUpload1.FileName;      // Append the name of the file to upload to the path.
          savePath += fileName;
          // Call the SaveAs method to save the 
          // uploaded file to the specified path.
          // This example does not perform all
          // the necessary error checking.               
          // If a file with the same name
          // already exists in the specified path,  
          // the uploaded file overwrites it.
          FileUpload1.SaveAs(savePath);      // Notify the user of the name of the file
          // was saved under.
          UploadStatusLabel.Text = "Your file was saved as " + fileName;
        }
        else
        {      
          // Notify the user that a file was not uploaded.
          UploadStatusLabel.Text = "You did not specify a file to upload.";
        }  }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>FileUpload Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
           <h4>Select a file to upload:</h4>       <asp:FileUpload id="FileUpload1"                 
               runat="server">
           </asp:FileUpload>       <br /><br />       <asp:Button id="UploadButton" 
               Text="Upload file"
               OnClick="UploadButton_Click"
               runat="server">
           </asp:Button>           <hr />       <asp:Label id="UploadStatusLabel"
               runat="server">
           </asp:Label>        
        </div>
        </form>
    </body>
    </html>
      

  5.   

    if (FileUpload1.PostedFile.FileName == "")
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", "alert('请选择要传的图片!');", true);
                        return;
                    }                string filePath = FileUpload1.PostedFile.FileName;//获取图片文件的路径
                    string imgname = filePath.Substring(filePath.LastIndexOf('/') + 1);
    //如果不需要重命名的话就直接取imgname这个值就是文件名
                    string imgtype = filePath.Substring(filePath.LastIndexOf('.') + 1).ToLower();
                    string savePath = "";//图片要保存的路径
                    string filename = "";//文件名
                    if (imgtype == "jpg" || imgtype == "jpeg" || imgtype == "gif" || imgtype == "bmp")
                    {
                        filename = DropDownListSort.SelectedValue + "_" + DropDownListBrand.SelectedValue + "_" + DateTime.Now.ToFileTime().ToString() + "." + imgtype;//给图片重命名
                        savePath = Server.MapPath("~/images/Goods/" + filename);//要保存的物理文件路径
                        FileUpload1.PostedFile.SaveAs(savePath);//保存图片
                    }
      

  6.   

    估计lz是不想写了,直接要一个程序,一复制粘贴就OK了!
      

  7.   

    String FilePath=FileUpload1.PostFile.FileName;
    String Filename=FilePath.SubString(FilePath.LastIndexOf(“\\”)+1;
    String ServerPath=Server.MapPath(@”~\...\ftp\”)+Filename;
    FileUpload1.PostFile.SaveAs(ServerPath);