asp.net C#(HtmlInputFile)上傳圖片代碼

來源:互聯網
上載者:User

asp教程.net c#(htmlinputfile)上傳圖片代碼

先看一下htmlinputfile檔案上傳功能
 
httpfilecollection   files     =   httpcontext.current.request.files;
if   (   files.count   ==   1   )
{
      httppostedfile   postedfile   =   files[0];
      if   (   postedfile.filename   !=   " "   )
      {
            string   filename   =   path.getextension(postedfile.filename);
            string   savepath   =   "/upload/ "   +   filename;
            try
            {
postedfile.saveas(savepath);
            }
            catch
            {
this.showmessage( "上傳照片失敗! ");
throw;
            }
    }
}


下面一款應用執行個體
%@page language="c#" %>
<%@import namespace="system.io"%>
<%@import namespace="system.data"%>
<%@import namespace="system.data.sqlclient"%>
<script language="c#" runat="server">
public void button_submit(object o, eventargs e)
{
        httppostedfile upfile = up_file.postedfile;
        int ifilelength = upfile.contentlength;
        try
        {
                if(ifilelength == 0)
                {
                        txtmess.text = "請選擇要上傳的檔案!";
                }
                else
                {
                        byte[] filebytearray = new byte[ifilelength];
                        stream streamobject = upfile.inputstream;
                        streamobject.read(filebytearray, 0, ifilelength);
                        sqlconnection conn = new sqlconnection("server=yy;uid=sa;pwd=;database=pany");
                        string sql = "insert into t_imgs (imgdata, type, description, imgsize) values "
                                + "(@image, @contenttype, @imagedescription, @imgsize)";
                        sqlcommand cmd = new sqlcommand(sql, conn);
                        cmd.parameters.add("@image", sqldbtype.binary, ifilelength).value = filebytearray;
                        cmd.parameters.add("@contenttype", sqldbtype.varchar, 50).value = upfile.contenttype;
                        cmd.parameters.add("@imagedescription", sqldbtype.varchar, 200).value = txtdesc.text;
                        cmd.parameters.add("@imgsize", sqldbtype.bigint, 8).value = upfile.contentlength;
                        conn.open();
                        cmd.executenonquery();
                        conn.close();
                        txtdesc.text = "";
                        txtmess.text = "ok!你已經成功上傳了類型的檔案";
                }
        }
        catch(exception ex)
        {
                txtmess.text = ex.message.tostring();
        }
}

                               
                       
</script>

<html>
<head>
        <title>上傳圖片</title>
</head>
<body bgcolor="#fffffa">
<form enctype="multipart/form-data" runat="server" id="form1">
<table runat="server" width=700 align=left id="table1" cellpadding=0 cellspacing =0 border=0>
        <tr>
                <td>上傳圖片</td>
                <td>
                <input type="file" id="up_file" runat="server" style="width:320" accept="text/*" name="up_file">
                </td>
        </tr>
        <tr>
                <td>檔案說明</td>
                <td>
                <asp:textbox runat="server" width=230 id="txtdesc" maintanstate="false" />
                </td>
        </tr>
        <tr>
                <td>
                <asp:label runat="server" id="txtmess" forecolor=red maintainstate="false" />
                </td>
                <td>
                <asp:button runat="server" width=230  text="上傳" />
                </td>
        </tr>
</table>
</form>
</body>
</html>

               
//顯示圖片

<%@page language="c#"%>
<%@import namespace="system.data"%>
<%@import namespace="system.data.sqlclient"%>
<script language="c#" runat="server">
public void page_load(object o, eventargs e)
{
        int imgid = convert.toint32(request.params["id"]);
        string connstr = configurationsettings.apps教程ettings["connectionstring"];
        sqlconnection conn = new sqlconnection(connstr);
        string sql = "select * from t_imgs where id = @imgid";
        sqlcommand cmd = new sqlcommand(sql, conn);
        cmd.parameters.add("@imgid", sqldbtype.int).value = imgid;
        conn.open();
        sqldatareader read = cmd.executereader();
        read.read();
        response.contenttype = (string)read["type"];
        response.outputstream.write((byte[])read["imgdata"], 0, (int)read["imgsize"]);
        response.end();
        conn.close();
}
</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.