本文转载自: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. ArrayList扩容

    jdk1.5 public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new Illega ...

  2. sqlyog注册码

    姓     名(Name):ttrar 序 列 号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c或者(OR)姓     名(Name):ttrar序 列 号(C ...

  3. POJ 2195 Going Home(费用流)

    http://poj.org/problem?id=2195 题意: 在一个网格地图上,有n个小人和n栋房子.在每个时间单位内,每个小人可以往水平方向或垂直方向上移动一步,走到相邻的方格中.对每个小人 ...

  4. key寻址算法

    分布式寻址算法 hash 算法(大量缓存重建) 一致性 hash 算法(自动缓存迁移)+ 虚拟节点(自动负载均衡) redis cluster 的 hash slot 算法 hash 算法 来了一个 ...

  5. PythonInstaller编译EXE方法+编译过程出错方案大全

    https://www.cnblogs.com/gopythoner/p/6337543.htmlhttps://www.zhihu.com/question/22963200https://blog ...

  6. Rails Guide -- Ruby on Rake(未详细阅读)

    一个软件task管理和build 自动化的工具. 它允许用户指定tasks和describe dependencies, 也可以在一个namespace中group tasks. 使用Ruby语言写的 ...

  7. PistgreSQL9.6手册(基础摘录)

    学习目的:基础使用. 能够开发RoR就行. git: https://github.com/postgres-cn/pgdoc-cn 1.2. 架构基础 PostgreSQL使用一种客户端/服务器的模 ...

  8. java 替换字符串中的中括号

    正确方式:"[adbdesf]".replaceAll("\\[", "").replaceAll("\\]", &qu ...

  9. restframework api (一)认证

    一 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度 ...

  10. js数字进制转换

    其他进制转十进制: 使用 parseInt()函数,parseInt解析一个字符串参数,并返回一个指定基数的整数 ,用法如下: parseInt(string, radix); 以二进制为例,用法如下 ...