表单发送文件及加自定义参数

/****************************************************************************************
** 作者: Eddie Xu 
** 时间: 2017/12/7 20:16:44
** 版本: V1.0.0
** CLR: 4.0.30319.42000
** GUID: c76c1823-494c-4fb3-8f3e-0d957eaa4089
** 机器名: DESKTOP-ECII567
** 描述: 尚未编写描述
****************************************************************************************/

using Manjinba.Communication.Common.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Manjinba.Communication.Common.Utils
{
public class HttpUploadUtil
{
private ArrayList bytesArray;
private Encoding encoding = Encoding.UTF8;
private string boundary = String.Empty;

public HttpUploadUtil()
{
bytesArray = new ArrayList();
string flag = DateTime.Now.Ticks.ToString("x");
boundary = "---------------------------" + flag;
}

/// <summary>
/// 合并请求数据
/// </summary>
/// <returns></returns>
private byte[] MergeContent()
{
int length = 0;
int readLength = 0;
string endBoundary = "--" + boundary + "--\r\n";
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);

bytesArray.Add(endBoundaryBytes);

foreach (byte[] b in bytesArray)
{
length += b.Length;
}

byte[] bytes = new byte[length];

foreach (byte[] b in bytesArray)
{
b.CopyTo(bytes, readLength);
readLength += b.Length;
}

return bytes;
}

/// <summary>
/// 上传
/// </summary>
/// <param name="requestUrl">请求url</param>
/// <param name="responseText">响应</param>
/// <returns></returns>
public bool Upload(String requestUrl, out String responseText)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);

byte[] responseBytes;
byte[] bytes = MergeContent();

try
{
responseBytes = webClient.UploadData(requestUrl, bytes);
responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
return true;
}
catch (WebException ex)
{
Stream responseStream = ex.Response.GetResponseStream();
responseBytes = new byte[ex.Response.ContentLength];
responseStream.Read(responseBytes, 0, responseBytes.Length);
}
responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
return false;
}
/// <summary>
/// 上传
/// </summary>
/// <param name="requestUrl">请求url</param>
/// <param name="responseText">响应</param>
/// <returns></returns>
public bool Upload(String requestUrl, long? rangeS, long? rangeE, out String responseText)
{
//WebClient webClient = new WebClient();
var webRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
try
{
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
//webRequest.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
if (rangeS != null && rangeE != null)
webRequest.AddRange(rangeS ?? 0, rangeE ?? 0);

//byte[] responseBytes;
byte[] bytes = MergeContent();
//responseBytes = webClient.UploadData(requestUrl, bytes);
webRequest.ContentLength = bytes.Length;
webRequest.Method = "POST";
using (Stream stream = webRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
responseText = streamReader.ReadToEnd();
return true;
}
catch (Exception e)
{
LogHelper.GetLog().Error(e.Message + e.StackTrace);
}
//catch (WebException ex)
//{
// Stream responseStream = ex.Response.GetResponseStream();
// responseBytes = new byte[ex.Response.ContentLength];
// responseStream.Read(responseBytes, 0, responseBytes.Length);
//}
//responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
responseText = string.Empty;
return false;
}

/// <summary>
/// 设置表单数据字段
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="fieldValue">字段值</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String fieldValue)
{
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
string httpRowData = String.Format(httpRow, fieldName, fieldValue);

bytesArray.Add(encoding.GetBytes(httpRowData));
}

/// <summary>
/// 设置表单文件数据
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="filename">字段值</param>
/// <param name="contentType">内容内型</param>
/// <param name="fileBytes">文件字节流</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes)
{
string end = "\r\n";
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string httpRowData = String.Format(httpRow, fieldName, filename, contentType);

byte[] headerBytes = encoding.GetBytes(httpRowData);
byte[] endBytes = encoding.GetBytes(end);
byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];

headerBytes.CopyTo(fileDataBytes, 0);
fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);

bytesArray.Add(fileDataBytes);
}

/// <summary>
/// 设置表单文件数据
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="filename">字段值</param>
/// <param name="contentType">内容内型</param>
/// <param name="fileBytes">文件字节流</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes, int partNum, long blocksize)
{
string end = "\r\n";
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; blocknum={3}; blocksize={4}; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string httpRowData = String.Format(httpRow, fieldName, filename, contentType, partNum, blocksize);

byte[] headerBytes = encoding.GetBytes(httpRowData);
byte[] endBytes = encoding.GetBytes(end);
byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];

headerBytes.CopyTo(fileDataBytes, 0);
fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);

bytesArray.Add(fileDataBytes);
}
}
}

原文地址:https://www.cnblogs.com/Nine4Cool/p/10540652.html

时间: 2024-08-20 00:14:17

表单发送文件及加自定义参数的相关文章

织梦cmsf表单提交到邮箱 织梦表单发送到邮箱 织梦自定义表单发邮箱

大家在做织梦做网站开发时会遇到一个问题:织梦的自定义表单是一个很鸡肋的功能,不仅在后台展示得奇丑,而且也没有提醒功能,使用起来很不方便.很多人用织梦自定义表单时,都想用户提交表单的时候可以发送到自己的邮箱里面去提醒自己.这样省了经常去织梦网站后台看最新的表单情况. 第一步:打开织梦后台,系统->核心参数 开启smtp 第二步: 修改/plus/diy.php代码.打开网站根目录下的plus文件 夹,里面有个diy.php文件(表单提交后处理文件),打开它,在85行上下有如下代码: $query

DedeCMS织梦自定义表单发送指定邮件

第一步.创建自定义表单 首先,我先自定义创建一个自定义表单,先把我们想要的属性,添加进行,然后完成后. 如下图: 1.先新建自定义表单,定义好前台页面.核心→频道模型→自定义表单→增加新的自定义表单 第二步.修改文件 打开网站根目录下的plus文件夹,里面有个diy.php文件(表单提交后处理文件),打开它,在85行上下有如下代码: $query = "INSERT INTO `{$diy->table}` (`id`, `ifcheck` $addvar)  VALUES (NULL,

Spring boot中普通工具类不能使用@Value注入yml文件中的自定义参数的问题

在写一个工具类的时候,因为要用到yml中的自定义参数,使用@Value发现值不能正常注入,都显示为null: yml文件中的自定义格式 调用工具类的时候不能new的方式 要使用@Autowired的方式注入进来, new会导致部分环境未加载,尽可能舍弃new的方式,交付spring管理 而工具类也是需要交给spring管理.需要在工具类上加上 @Component注解然后注意一下的是 在springframework下不能@Autowired静态变量 所以在变量上不能有 static 怎么扫描注

dedecms织梦自定义表单发送到指定邮箱(qq,163)

网上很多教程都是用dedecms自带的邮件库发送邮件,而且是写死要发送的字段内容,今天我们用phpmailer来给站长发送自定义表单提交的数据到指定邮箱上(QQ邮箱,163邮箱都可以),不写死任何东西,有什么字段就提交发送什么字段. 操作之前,我们要先搞定用来发送邮件的那个邮箱,说白了就是要开启SMTP. QQ篇 登录QQ邮箱-设置-账户,找到POP3/IMAP/SMTP,开启,现在的邮箱开启POP3/SMTP都要授权码了,千万要记下你的授权码啊,等一下是作为密码使用的. 开启成功后是这样的 1

php表单发送到邮箱V1.0

html表单代码: <form action="index.php" name="form" method="POST"> <ul > <input name="url" type="hidden" id="url"> <input name="ip" type="hidden" id="ip&q

from表单发送请求方式

1,直接用表单的 action 属性跳转路由,此方法必须各节点的name属性和 Java bean  实体类属性相对应,并且onsubmit 属性不为false. 优点:快速,简单跳转路由. 缺点:返回数据不好处理,前后台交互繁琐. <form name="mdShopuser.form" action="/ShopUser/ShopUserGoin" method="POST" autocomplete="off" o

php+socket模拟表单发送请求

1 <?php 2 /** 3 * http请求类(php + socket) 4 * @todo 这里还有很多未完善的地方,仅有简单的get post head请求 5 * @author [email protected] 6 * @version 1.0.0 7 */ 8 9 class HttpClient { 10 11 const CRLF = "\r\n"; 12 private $fh = null; //socket handle 13 private $err

form表单发送请求实例

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8" import="java.util.*,javax.faces.context.FacesContext,javax.faces.application.FacesMessage,javax.servlet.http.HttpServletRequest&q

跟我一起学extjs5(40--增加一个自定义模块[1建立表和bean文件])

跟我一起学extjs5(40--增加一个自定义模块[1建立表和bean文件]) 经过上面的工作,一个独立模块的CRUD已经可以运行了,为了更好的可以看到其他自定义功能,我们做一个自定义的独立模块,这个模块中包括了各种的数据类型. 新增一个"销售合同"的独立模块,模块名称为Agreement,里面包括了20几个字段,把主要的字段类型都设置了,有字符串型,整型,浮点型,金额型,日期型,布尔型,百分比,还包括计算字段. 建立表的sql语句如下: /****** 对象: Table [dbo]