模拟form表单请求上传文件
发请求
public string CameraFileUpload(string url,string path,string serverPath,string uploadfileName)
{ System.Net.ServicePointManager.DefaultConnectionLimit = ;
System.GC.Collect(); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.KeepAlive = false;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + ); //请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); request.Headers.Add("serverPath", serverPath);
request.Headers.Add("uploadfileName", uploadfileName); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, , bArr.Length);
fs.Close(); Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, , itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, , postHeaderBytes.Length);
postStream.Write(bArr, , bArr.Length);
postStream.Write(endBoundaryBytes, , endBoundaryBytes.Length);
postStream.Close(); //发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd(); if (request != null)
{
request.Abort();
request = null;
}
if (response != null)
{
response.Close();
response = null;
} return content;
}
处理请求:
protected void Page_Load(object sender, EventArgs e)
{
try
{
HttpPostedFile file = Request.Files[]; string temp = Request.Form["temp"];
string serverPath = Request.Headers["serverPath"];
string uploadfileName = Request.Headers["uploadfileName"]; filePathCheck(serverPath);
file.SaveAs(Server.MapPath(serverPath + "\\" + uploadfileName));
Response.Write("上传成功");
}
catch (Exception ex)
{
Response.Write("上传失败 原因:" + ex.Message);
}
} public void filePathCheck(string path)
{
if (!Directory.Exists(Server.MapPath(path)))
Directory.CreateDirectory(Server.MapPath(path));
}
模拟form表单请求上传文件的更多相关文章
- JAVA入门[16]-form表单,上传文件
一.如何传递参数 使用 @RequestParam 可以传递查询参数.例如:http://localhost:8092/category/detail?id=1 @RequestMapping(&qu ...
- Java模拟表单POST上传文件
JAVA模拟表单POST上传文件 import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.io ...
- Linux curl 模拟form表单提交信息和文件
Linux curl 模拟form表单提交信息和文件 curl是一个命令行方式下传输数据的开源传输工具,支持多种协议:FTP.HTTP.HTTPS.IMAP.POP3.TELNET等,功能超级强大 ...
- Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request对象方法,属性和Response对象,form表单的上传
Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request请求对象方法,属性和Response响应对象,form表单的上传 一丶CBV和FBV 在Django中存 ...
- js_ajax模拟form表单提交_多文件上传_支持单个删除
需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...
- 【温故知新】Java web 开发(三)Form表单与上传下载文件
简介:在一和二的基础之上,这次来记录下如何在页面提交表单数据,以及文件的上传和下载整个流程,请求也不仅限于GET了,也有POST了. 1. 为了方便,在 webapp 下直接新建一个 index.ht ...
- 通过C#的HttpClient模拟form表单请求
post提交表单一般无非是一般text文本和文件类型,如下 <input type="file"/> <input type="text"/& ...
- html-4, form 表单 输入、传文件、单选、多选、下拉菜单、文本描述、重置、submit、按钮限制输入
<!-- form HTTP协议 action:提交的服务器网址 method:get(默认)| post(应用:登录注册.上传文件) 页面中的a img link 默认是get请求 input ...
- web 表单方式上传文件方法(不用flash插件)
原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...
随机推荐
- webpack CommonsChunkPlugin 提取公共代码
1.项目结构 2.部分代码 module.js console.log('module.js'); index文件夹下的index.js require('../module.js'); consol ...
- Patterns-Proxy
http://blog.csdn.net/jianghuxiaoxiami/article/details/3403924 1.代理模式 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问 ...
- 大话设计模式C++实现-第1章-简单工厂模式
一.UML图 二.包括的角色 简单工厂模式包括三个角色: (1)工厂类Factory:工厂类是用来制造产品的. 因此,在Factory中有一个用于制造产品的Create函数或者Generate函数之类 ...
- Oracle Unicode转中文(解码)
Oracle Unicode转中文(解码) CreateTime--2018年3月29日15:23:30 Author:Marydon 情景描述: 将数据库中的某个字段误存储的是Unicode编码 ...
- Hibernate4.2.21.Final创建入门的HelloHibernet工程
1.在hibernate官网下载hibernate-release-4.2.21.Final.zip并解压 2.新建一个java project工程(HelloHibernet)(myeclipes) ...
- 利用DM工具Weka进行数据挖掘(分类)的完整过程
利用DM工具Weka进行数据挖掘(分类)的完整过程:
- C#:向SqlServer数据库中插入imange类型
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...
- 输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
一.以操纵子方式格式化 数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符.把它们作为插入操作符<<的输出对象即可.如setiosflags.setw.set ...
- 【Android UI设计与开发】使用ViewPager实现欢迎引导页面
一.实现的效果图 每次看别人的代码前,必需要先看实现的效果图达到了一个什么样的效果,是不是跟自己想要实现的效果类似.有图才有真相嘛,同一时候,最下方有源代码的下载地址,差点儿源代码的每一行都有凝视,写 ...
- IntelliJ IDEA 学习(一):IntelliJ IDEA15 破解方法(已验证)
新的破解方法: 1.进到文件夹中:C:\Windows\System32\drivers\etc ,找到hosts文件,用记事本编辑 2.如果没有找到hosts文件,可在查看设置中勾选“显示隐藏的项目 ...