当前位置:编程学习 > C#/ASP.NET >>

求图片上传

求asp.net图片上传代码,在数据库中存的是图片名,不是地址,存在电脑硬盘中.有合适的发在404956675@qq.com或写在楼下。。 --------------------编程问答-------------------- 图片上传?网上一搜一堆。。。。

保存图片名的时候。。你自己截取就是了。。。。 --------------------编程问答-------------------- 百度,google一下就一大堆了。  珍惜你的分数啊。
上传控件下面就直接有一个属性获得图片名的。 还有个属性是获得全路径。
--------------------编程问答-------------------- 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; 
        } 
    } 
--------------------编程问答--------------------

<%@ 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>

--------------------编程问答-------------------- 去你QQ邮箱里取吧! --------------------编程问答--------------------
引用 2 楼 runbear 的回复:
百度,google一下就一大堆了。  珍惜你的分数啊。
上传控件下面就直接有一个属性获得图片名的。 还有个属性是获得全路径。

是啊,不要用来浪费。真的,网上的资料超级多啊,各式各样的上传都有。 --------------------编程问答-------------------- <%@ 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>


--------------------编程问答--------------------
引用 3 楼 wuyq11 的回复:
protected void btnUpload_Click(object sender, EventArgs e) 
    { 
        if (fileUpload.HasFile) 
        { 
            string savePath = Server.MapPath("~/upload/"); 
            if(!System.……

顶,这个就可以了
但是图片不能太大,好像超过2M就上传不上去了吧。 --------------------编程问答-------------------- 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);//保存图片
                } --------------------编程问答--------------------
引用 4 楼 lq_651119244 的回复:
HTML code

<%@ 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">

    pr……



估计lz是不想写了,直接要一个程序,一复制粘贴就OK了! --------------------编程问答-------------------- lz是男是女??
右耳朵旁边还有两缕发丝... --------------------编程问答-------------------- 答案很多了,我就来接分吧! --------------------编程问答-------------------- 去取吧! --------------------编程问答-------------------- 网上很多的,不需要花时间,花分等答案 --------------------编程问答-------------------- String FilePath=FileUpload1.PostFile.FileName;
String Filename=FilePath.SubString(FilePath.LastIndexOf(“\\”)+1;
String ServerPath=Server.MapPath(@”~\...\ftp\”)+Filename;
FileUpload1.PostFile.SaveAs(ServerPath);
--------------------编程问答-------------------- 网上有很多,我以前自己做的都是在网上找的,要是要我的,明天给你 --------------------编程问答-------------------- 网上有很多,给个网址! --------------------编程问答--------------------
引用 3 楼 wuyq11 的回复:
protected void btnUpload_Click(object sender, EventArgs e) 
  { 
  if (fileUpload.HasFile) 
  { 
  string savePath = Server.MapPath("~/upload/"); 
  if(!System.IO.Directory.Exists(savePath)) 
  ……



正解
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,