wcf 多文件上传
wcf rest主要是走路由来开发的API,配置路由:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
//测试
RouteTable.Routes.Add(new ServiceRoute("SendInfo/testInfo", new WebServiceHostFactory(), typeof(WebWcfRest.SendInfo.testInfo)));
//功能接口 查询
RouteTable.Routes.Add(new ServiceRoute("WCF/DataBase/IDbHelper", new WebServiceHostFactory(), typeof(WebWcfRest.WCF.DataBase.IDbHelper)));
//极光推送
RouteTable.Routes.Add(new ServiceRoute("WCF/AppApi/IApiOA", new WebServiceHostFactory(), typeof(WebWcfRest.WCF.AppApi.IApiOA)));
}
}
接口方法类:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[JavascriptCallbackBehavior(UrlParameterName = "callback")]
public class IApiOA
{
#region 极光
/// <summary>
/// 批量推送消息
/// 接口说明:Y 为必填 N 为可选填写
/// </summary>
/// <param name="toUserID">将要发送给用户的userid 可为数组(,分割) (Y) </param>
/// <param name="title">推送 标题 (N) </param>
/// <param name="alert">推送 alert 信息 (Y) </param>
/// <param name="msg_content">推送内容 (N) </param>
/// <param name="RegistratingID">推送设备号 (N) </param>
/// <returns>json 数据 </returns>
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "PushApp?toUserID={toUserID}&title={title}&alert={alert}&msg_content={msg_content}&RegistratingID={RegistratingID}")]
public Stream PushApp(string toUserID, string title, string alert, string msg_content, string RegistratingID)
{
string json = "";
StringBuilder str = new StringBuilder();
int num = 0;
bool state = false;
if (!String.IsNullOrEmpty(toUserID))
{
//str.Append("001");
var toUserIDList = toUserID.Split(',').ToList();
if (toUserIDList.Count > 0)
{
//str.Append("002");
for (int i = 0; i < toUserIDList.Count; i++)
{
//str.Append("003");
//拿到userID 后获取用户设备号 RegistratingID
////----SqlDataProvider provider = new SqlDataProvider();
PageList_IDAO pageList = new PageList_Dal();
string sql = " select * from Base_UserInfo where DZM_ID = " + toUserIDList[i];
////----DataTable dt = provider.SqlDataTable(sql);
DataTable dt = pageList.SelectSql(sql);
if (dt.Rows[0]["RegistratingID"].ToString() != "")
{
//str.Append("004");
bool bol = SwtichList(toUserIDList[i], title, alert, msg_content, dt.Rows[0]["RegistratingID"].ToString());
//str.Append("005");
if (bol)
{
//str.Append("006");
str.Append(toUserIDList[i]);
}
}
num = i;
}
state = true;
}
}
//str.Append(toUserID + "---" + alert);
json = "{\"state\":\"" + state + "\",\"number\":\"" + (num + 1) + "\",\"toUserID\":\"" + str.ToString() + "\",\"CDATA\":\"[{\"state\":\"返回状态 if 成功 ? true : false \",\"number\":\"修改成功个数\"}]\"}";
//return json;
return new MemoryStream(Encoding.UTF8.GetBytes(json));
}
}
}
可GET post put 等 其他一般用不到
多文件上传文件流代码:
/// <summary>
/// 遍历循环 附件上传文件方法
/// </summary>
/// <param name="actionUpload"></param>
/// <param name="pageFileP"></param>
/// <param name="fileName"></param>
/// <param name="lengthList"></param>
/// <param name="filestream"></param>
/// <param name="bol"></param>
/// <returns></returns>
public string SendOAInfoFileNameT_Get(string actionUpload, string pageFileP, string fileName, string lengthList, Stream filestream, ref bool bol, ref string EFName, ref string CFName)
{
StringBuilder str = new StringBuilder();
string json = "";
bol = false;
EFName = "";
CFName = "";
//bool bol = false;
try
{
//var filestreamL = filestream.Length;
PageList_IDAO pageList = new PageList_Dal();
if (actionUpload != null && actionUpload == "Upload")
{
if (!Directory.Exists(pageFileP))//存放的默认文件夹是否存在
{
Directory.CreateDirectory(pageFileP);//不存在则创建
}
//
fileName = HttpUtility.UrlDecode(fileName); //再次解码一次
//
var listLengthSplit = lengthList.Split(',');//判断是否有多个文件 需要上传不?
if (listLengthSplit.Length > 0)
{
var dir = System.Web.HttpContext.Current.Server.MapPath("~/Files");
//var path = Path.Combine(dir, pageFileP + "\\" + fileName);
var fileNameSplit = fileName.Split(',');
byte[] m_Bytes = ReadStreamTobyte(filestream);
for (int i = 0; i < listLengthSplit.Length; i++)
{
var listLengthSplitBFH = listLengthSplit[i].Split('_');
//var path = Path.Combine(dir, pageFileP + "\\" + fileNameSplit[i]); //路径 带文件后缀 true
//文件名称 带后缀 start
var strSplit = fileNameSplit[i].Split('.');
var nameFile = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + i + "." + strSplit[1];
str.Append(",\"" + i + "_NUM\":\"" + nameFile + "\"");
var path = Path.Combine(dir, pageFileP + "\\" + nameFile); //路径 带文件后缀 false
//文件名称 带后缀 end
int len = (int)m_Bytes[0];//byte长度
byte[] data = cutOutByteSub(m_Bytes, int.Parse(listLengthSplitBFH[0]), int.Parse(listLengthSplitBFH[1]));
fileStreamByteSwitch(data, path);
//
//EFName CFName
CFName += fileNameSplit[i] + "|";
EFName += nameFile + "|";
}
}
/////直接存,留待以后数据对接用
//using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
//{
// byte[] filecontent = m_Bytes;
// BinaryWriter bw = new BinaryWriter(fs);
// bw.Write(filecontent);
// bw.Close();
// fs.Close();
//}
}
else
{
json = "{\"error\":\"josn格式错误\"}";
}
json = "{" + str.ToString().Substring(1, str.ToString().Length - 1) + "}";
bol = true;
}
catch (Exception e)
{
bol = false;
json = "{\"error\":\"josn格式错误\"}";
}
// return new MemoryStream(Encoding.UTF8.GetBytes(json));
return json;
}
/// <summary>
/// * 截取byte数据 主要满足多文件字节流
/// </summary>
/// <param name="b"></param>
/// <param name="startLength">起始下标</param>
/// <param name="endLength">结束 长度</param>
/// <returns></returns>
public static byte[] cutOutByteSub(byte[] b, int startLength, int endLength)
{
if (b.Length == 0 || endLength == 0 || b.Length < (startLength + endLength))
{
return null;
}
byte[] bjq = new byte[endLength];
int j = 0;//因为从0开始 故重新取J来做下标
for (int i = startLength; i < (endLength + startLength); i++)
{
bjq[j] = b[i];
j++;
}
return bjq;
}
---------------------
作者:silly-0107
来源:CSDN
原文:https://blog.csdn.net/shamao1113/article/details/48442441
版权声明:本文为博主原创文章,转载请附上博文链接!