本文转载自:http://hi.baidu.com/love_1210/item/120e452b42b2a854c38d59eb

客户端代码:

<form name="form1" method="post" enctype="multipart/form- data" action="requestfile/asprece.aspx">//如果file框没有加runat="server",则 form里一定要加上 enctype="multipart/form-data"这样才可以实现上传文件到服务器;使用了server和没有使用

runat="server"是有区别的.使用了runat="server"的form编译后,action必定是指向本身的网页。而没

有加runat="server"的form可以指向一个网页。

<input type="file" name="file1" style="width:160px;" />
        <input type="submit" name="Submit" value="添加" />
    </form>

服务器端代码:

private string retvalue = "ok";
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                HttpPostedFile req = Request.Files["file1"];
                if (req == null || req.ContentLength < 0)
                {

Response.Write("没有文件");
                    Response.End();
                }
                else
                {
                    try
                    {

string extion = System.IO.Path.GetExtension(req.FileName.ToString());
                        string date = DateTime.Now.ToString("yyyyMMddhhmmss").ToString();
                        string src = date + extion;
                        string pathnew = Server.MapPath("~/testfile/");
                        req.SaveAs(pathnew+src);        //自带的方式保存文件

/*读取文件流保存

Stream stream = req.InputStream;
                        //string src = "test.xls";
                        string fullpathnew = pathnew + src;

if (!Directory.Exists(pathnew))
                        {
                            Directory.CreateDirectory(pathnew);
                        }

BinaryReader br = new BinaryReader(stream);
                        byte[] fileByte = br.ReadBytes((int)stream.Length);
                        // string content = fileByte.ToString();
                        using (FileStream fileStream = new FileStream(fullpathnew, FileMode.Create))
                        {
                            fileStream.Write(fileByte, 0, fileByte.Length);
                        }*/

}
                    catch (Exception es)
                    {
                        retvalue = es.Message.ToString();

}
                    finally 
                    {
                        Response.Write(retvalue);
                    }
                }
            }

(转)html中使用表单和input file上传图片的更多相关文章

  1. ajax form表单提交 input file中的文件

    ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了 ...

  2. Django中使用表单

    使用表单 表单用 user 提交数据,是网站中比较重要的一个内容 GET 和 POST 方法 GET 和 POST 的区别 URL,全称是"统一资源定位符".用于对应互联网上的每一 ...

  3. input file 在开发中遇到的问题 类似ajax form表单提交 input file中的文件

    最近在做项目的过程中遇到个问题,在这里做个记录防止日后忘记 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为 ...

  4. ASP.NET MVC中使用表单上传文件时的注意事项

    最近了好久没写ASP.NET 使用HTML的FORM来上传文件了,结果写了个文件上传发现ASP.NET MVC的Controller中老是读取不到上传的文件. MVC的View(Index.cshtm ...

  5. JQuery input file 上传图片

    表单元素file设置隐藏,通过其他元素打开: .imgfile为input file $(".ul").click(function () {return $(".img ...

  6. element-ui中使用表单验证的问题

    <el-form ref="ruleRules" :inline="true" :model="ruleInfo"> <e ...

  7. input file 上传图片问题

    html代码如下: <input id="fileup" type="file" accept="image/*" capture=& ...

  8. html5手机 input file 上传图片 调用API

    <input type="file" accept="video/*;capture=camcorder"> <input type=&quo ...

  9. input file上传图片预览,非插件

    Input标签 <input type="file" name="pic" onchange="changepic(this)" mu ...

随机推荐

  1. Ubuntu16.04下安装tensorflow(GPU加速)【转】

    本文转载自:https://blog.csdn.net/qq_30520759/article/details/78947034 版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...

  2. CentOS7.2 安装Tomcat

    Centos默认安装JDK 现在要删除旧版本的jdk,安装新版本jdk 查看现有jdk: [root@localhost 桌面]# rpm -qa | grep jdk java-1.8.0-open ...

  3. 如何利用Xshell在windows与linux之间互传文件

    如何利用Xshell在windows与linux之间互传文件 第一步: 安装Xshell. 第二步: 打开Xshell,若出现默认的对话框,则选择关闭,因为下面将演示如何将本地文件传输至远程linux ...

  4. SDN前瞻 网络的前世今生

    本文基于SDN导论的视频而成:SDN导论 目前网络层面流行的技术概念:虚拟中心:公有云私有云:数据中心等等. SDN主要的模拟器:Mininet OpenDaylight(Cisco) ONOS(AT ...

  5. HDU 4489 The King’s Ups and Downs

    http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意:有n个身高不同的人,计算高低或低高交错排列的方法数. 思路:可以按照身高顺序依次插进去. d[i][ ...

  6. nil 作比较时应该加上双引号 "

    > type(X) nil > type(X)==nil false > type(X)=="nil" true >

  7. Qt5.4.1_静态编译

    http://www.cnblogs.com/findumars/p/4852350.html http://godebug.org/index.php/archives/133/ http://ww ...

  8. 动态规划-Predict the Winner

    2018-04-22 19:19:47 问题描述: Given an array of scores that are non-negative integers. Player 1 picks on ...

  9. 056——VUE中vue-router之路由参数的验证处理保存路由安全

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 看我如何快速学习.Net(高可用数据采集平台)

    最近文章:高可用数据采集平台(如何玩转3门语言php+.net+aauto).高并发数据采集的架构应用(Redis的应用) 项目文档:关键词匹配项目深入研究(二)- 分表思想的引入 吐槽:本人也是非常 ...