1  /  1  页   1 跳转 查看:120

上传图片

上传图片

想要加一项能够上传图片的功能  ,希望各位帮帮忙
后台代码
public partial class addproduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Random rnd = new Random();
        TextBox7.Text = rnd.Next(1000, 8000).ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection("Data source=localhost;uid=sa;pwd=;database=Ebusiness");
        SqlCommand objsqlcomm = new SqlCommand();
        objsqlcomm.Connection = sqlcon;
        objsqlcomm.CommandType = CommandType.StoredProcedure;
        objsqlcomm.CommandText = "insert_product";
        objsqlcomm.Parameters.Add("@num", SqlDbType.Int).Value = TextBox1.Text;
        objsqlcomm.Parameters.Add("@petname", SqlDbType.Char, 20).Value = TextBox2.Text;
        objsqlcomm.Parameters.Add("@petmoney", SqlDbType.Money).Value = TextBox3.Text;
        objsqlcomm.Parameters.Add("@petlocal", SqlDbType.Char,50).Value = TextBox4.Text;
        objsqlcomm.Parameters.Add("@aboutpet", SqlDbType.Char, 2000).Value = TextBox5.Text;
        objsqlcomm.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Now.ToString();
        objsqlcomm.Parameters.Add("@return_value", SqlDbType.Int);
        objsqlcomm.Parameters["@return_value"].Direction = ParameterDirection.ReturnValue;
        int i_ret = -100;
        try
        {
            sqlcon.Open();
            objsqlcomm.ExecuteNonQuery();
            i_ret = (int)objsqlcomm.Parameters["@return_value"].Value;
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
        finally
        {
            sqlcon.Close();
            Label1.Text = "添加成功,点击此处进入产品管理";
        }
        clean();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        clean();
    }
    private void clean()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
    }
}

前台代码
<body >
    <form id="form1" runat="server">
    <div>
              <center>
          </center>
    <center>
    <table border=2>
    <tr>
    <th style="width: 146px; height: 28px;" colspan=2 align=center valign="top">
                      <em><span style="font-size: 13.55pt">
            添加</span></em></th>
    </tr>
    <tr>
    <td style="width: 146px; height: 49px">
        <strong><em><span style="font-size: 13.55pt">数量</span></em></strong>:(*)
    </td>
    <td style="width: 489px; height: 49px" align="center" valign="top">
         
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" C
            ErrorMessage="RequiredFieldValidator">不能为空</asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td style="width: 146px; height: 54px">
        <strong><em><span style="font-size: 13.55pt">名称</span></em></strong>:(*)
    </td>
    <td style="width: 489px; height: 54px" valign="top">
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" C
            ErrorMessage="RequiredFieldValidator">不能为空</asp:RequiredFieldValidator></td>
    </tr>
    <tr>
    <td style="width: 146px; height: 57px">
        <strong><em><span style="font-size: 13.55pt">价格</span></em></strong>:(*)
    </td>
    <td style="width: 489px; height: 57px" valign="top">
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" C
            ErrorMessage="不能为空"></asp:RequiredFieldValidator></td>
    </tr>
    <tr>
    <td style="width: 146px; height: 58px">
        <strong><em><span style="font-size: 13.55pt">产地</span></em></strong>:(*)
    </td>
    <td style="width: 489px; height: 58px" valign="top">
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" C
            ErrorMessage="RequiredFieldValidator">不能为空</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
    <td style="width: 146px; height: 51px">
        <strong><em><span style="font-size: 13.55pt">介绍</span></em></strong>:
    </td>
    <td style="width: 489px; height: 51px" valign="top">
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></td>
    </tr>
        <tr>
    <td style="width: 146px; height: 58px">
    验证码:
    </td>
    <td style="width: 489px; height: 58px" valign="top">
             
        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator2" runat="server" C
            C ErrorMessage="CompareValidator">请输入验证码</asp:CompareValidator></td>
    </tr>
    <tr>
    <td style="width: 146px; height: 31px" colspan=2 align=center valign="top">
        <asp:TextBox ID="TextBox7" runat="server" BackColor="Red" BorderColor="Black"></asp:TextBox> </td>
    </tr>
    </table>
 
    <table>
    <tr>
    <td style="width: 90px">
    <asp:Button ID="Button1" runat="server" Text="提交" Width="61px"  />
    </td>
    <td style="width: 100px">
    <asp:Button ID="Button2" runat="server" Text="重置"  Width="59px" />
    </td>
    </tr>
    </table>
    </center>
    </div>
    </form>
</body>
 

回复: 上传图片

你放个fileupload控件,然后saveas不就行了
自己看看相关的东西把
 

回复 1F lcc100 的帖子

受累给写写,要传倒数数据库,我刚学,看看怎么弄啊
 

回复:上传图片

保存到数据库主要在要插入图片的表设定这三个字段:图像二进制数据PictureData,图片类型PictureType,图片大小pt.PictureSize
以下是操作方式:
Byte[] FileByteArray = new Byte[FileUpload.PostedFile.ContentLength]; //图象文件临时储存Byte数组,FileUpload是浏览控件

Stream StreamObject = FileUpload.PostedFile.InputStream; //建立数据流对像

StreamObject.Read(FileByteArray,0,FileUpload.PostedFile.ContentLength);//读取图象文件数据,FileByteArray为数据储存数组,0为数据指针位置、FileUpload.PostedFile.ContentLength为数据长度

然后把二进制数据,类型,大小这三个东东存进数据库就OK了。
        pt.PictureData = FileByteArray;
        pt.PictureType=FileUpload.PostedFile.ContentType;
        pt.PictureSize=FileUpload.PostedFile.ContentLength;
pt是图片的实体类,点后面是跟数据库字段对应的实体类属性。其他操作跟你写的一样。
本帖被评分 1 次
 

回复:上传图片

谢谢
但是我 想要的是 先将图片存到指定的文件加,数据库中留路径
显示的时候直接通过路径显示
 

回复: 上传图片

那要看你的图片具体路径是在哪里。

先引用using System.IO;保存路径时,

用string ss = Server.MapPath("../Images/") + Path.GetFileName(FileUpload.PostedFile.FileName);

(关于Server.MapPath()的用法知道吧?)

再把ss存进数据库对应的字段。

显示图片时,给图像控件的路径赋值就可以了。

Img.ImageUrl = 从数据库读出来的字段值。
本帖被评分 1 次
 
1  /  1  页   1 跳转

版权所有 ASP.NET学习门户 2.0.1214   Sitemap  

返顶部