还得麻烦你一件事asp.net 图片上传怎么实现?要代码!我以前是有winform实现的上传,现在想用asp.net的网页实现,谢谢!

解决方案 »

  1.   

      FileUpload 控件 <asp:FileUpload ID="fileHeader" runat="server" />cs代码:string path = Server.MapPath("~/plmnhytf12f3");
            string header = "";
            if (fileHeader.FileName != "")
            {            path = path + "/jobs/" + id + "/";
                header = "/plmnhytf12f3/jobs/" + id + "/" + fileHeader.FileName;
                string sql = "update vogues set header='" + header + "' where id=" + id;
                DbHelperSQL.GetSingle(sql);            Directory.CreateDirectory(path);
                fileHeader.SaveAs(path+fileHeader.FileName);
            }
    自己看着整呗
      

  2.   

    图片存储:通过文件上传获取图片并转换成Byte[]字节数组,保存到数据库Image字段
    昨晚给你整的读取,我以为你会图片存入数据库呢,下面的根据你需要进行改改,就是数据插入那块,根据你实际字段来。
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!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" style="font-size: 12px;" enctype="multipart/form-data">
        备 注:<asp:TextBox ID="name" runat="server"></asp:TextBox>
        上 传:<asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClientClick="return checkClint()" Text="上 传"
            OnClick="Button1_Click" />
        <script language="javascript" type="text/javascript">
            function checkClint() {
                var getname = document.getElementById("name");
                var getfile = document.getElementById("FileUpload1");            if (getname.value == "") {
                    alert('请输入图片备注名称!');
                    getname.focus();
                    return false;
                } else if (getfile.value == "") {
                    alert('请选择上传文件路径!');
                    getfile.focus();
                    return false;
                } else {
                    return true;
                }
            } 
        </script>
        </form>
    </body>
    </html>
    ----------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Data.SqlClient;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取数据
            string getname = this.name.Text;
            string getfile = this.FileUpload1.PostedFile.FileName;        //上传文件
            string getlastpath = FileUploadCompant(this.FileUpload1);        //获取上传文件流
            byte[] getbyte = new byte[this.FileUpload1.PostedFile.ContentLength];
            Stream filestream = this.FileUpload1.PostedFile.InputStream;        //读入数据
            filestream.Read(getbyte, 0, this.FileUpload1.PostedFile.ContentLength);        //插入数据
            #region
            string sql = "insert into employee(name,Content,Type,Size,LinkUrl) values(@name,@content,@type,@size,@link)";        SqlParameter[] getpars = new SqlParameter[5];
            getpars[0] = new SqlParameter("@name", getname);
            getpars[1] = new SqlParameter("@content", getbyte);//文件内容插入This.Fileupload1.FileBytes同样可以直接转换成Byte数组不用转换
            getpars[2] = new SqlParameter("@type", this.FileUpload1.PostedFile.ContentType);//保存文件类型
            getpars[3] = new SqlParameter("@size", this.FileUpload1.PostedFile.ContentLength);//文件长度
            getpars[4] = new SqlParameter("@link", getlastpath);        int getrescount = DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.connString, CommandType.Text, sql, getpars);        if (getrescount == 1)
            {
                //添加成功
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '图片记录成功添加到数据库'); ", true);
            }
            else
            {
                //添加失败
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '图片记录添加失败'); ", true);
            }        #endregion
        }    /// <summary>
        /// 文件上传操作-单独隔离出来方法
        /// </summary>
        public string FileUploadCompant(FileUpload getfileupload)
        {
            string takeServerpath = string.Empty;//保存到服务器路径        try
            {
                if (string.IsNullOrEmpty(getfileupload.PostedFile.FileName))
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '请选择上传文件的路径!'); ", true);
                    getfileupload.Focus();
                }
                else
                {
                    string filepath = getfileupload.PostedFile.FileName;//获得全路径
                    //获得文件名
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);                //保存服务器路径 -唯一文件名
                    string serverpath = Server.MapPath("FileuploadDict/") + System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss") + Session.SessionID + filename;                //保存到服务器上
                    getfileupload.SaveAs(serverpath);                //保存成功 提示信息
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '文件上传成功!'); ", true);
                    takeServerpath = serverpath;
                }
            }
            catch (Exception se)
            {
                //抓取一场信息 并提示
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '文件上传失败-上传过程出现如下异常信息:" + se.Message.ToString() + "!'); ", true);
            }        return takeServerpath;
        }
    }