【www.gdgbn.com--php常用代码】

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>



        上传图片




       
                上传图片
               
               
               
       
       
                文件说明
               
               
               
       
       
               
               
               
               
               
               
       



               
//显示图片

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

本文来源:http://www.gdgbn.com/jiaocheng/29423/