那位做过移动Web应用程序,小弟请教一下手机终端上传附件到服务器是怎么解决的?请给点页面和后台代码

qwx99 2006-06-05 10:39:55
那位做过移动Web应用程序,小弟请教一下手机终端上传附件到服务器是怎么解决的?请给点页面和后台代码
...全文
424 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyherocn 2006-10-01
  • 打赏
  • 举报
回复
今天终于看到了这个代码了~~
redfox105 2006-09-15
  • 打赏
  • 举报
回复
给个例子参考(桌面的,移动的也可以参照这个方式)

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Collections;
namespace Torch.DotNet
{
/// <summary>
/// CreateFormData 的摘要说明。
/// </summary>
public class CreateFormData
{
Encoding encoding = Encoding.UTF8;
public CreateFormData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 类属性
public string ContentType
{
get
{
if (HttpContext.Current == null)
{
return "multipart/form-data; boundary=---------------------------7d5b915500ceetorch";
}
return HttpContext.Current.Request.ContentType;
}
}
public string Boundary
{
get
{
string contentType = ContentType;
string[] bArray,ctArray;
ctArray = contentType.Split(';');
if (ctArray[0].Trim().ToLower() == "multipart/form-data")
{
bArray = ctArray[1].Split('=');
return "--"+bArray[1];
}
return null;
}
}
#endregion
/// <summary>
/// 获取表单域的二进制数据
/// </summary>
/// <param name="fieldName">表单域名称</param>
/// <param name="fieldValue">表单域的值</param>
/// <returns>二进制数组</returns>
public byte[] CreateFieldData(string fieldName,string fieldValue)
{
string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
string text = String.Format(textTemplate, fieldName, fieldValue);
byte[] bytes = encoding.GetBytes(text);
return bytes;
}
/// <summary>
/// 获取表单文件上传域的二进制数组
/// </summary>
/// <param name="fieldName">表单域名称</param>
/// <param name="filename">文件名称</param>
/// <param name="contentType">文件类型</param>
/// <param name="fileBytes">文件流</param>
/// <returns>二进制数组</returns>
public byte[] CreateFieldData(string fieldName, string filename,string contentType, byte[] fileBytes)
{
string end = "\r\n";
string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

// 头数据
string data = String.Format(textTemplate, fieldName, filename, contentType);
byte[] bytes = encoding.GetBytes(data);



// 尾数据
byte[] endBytes = encoding.GetBytes(end);

// 合成后的数组
byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];

bytes.CopyTo(fieldData, 0); // 头数据
fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // \r\n

return fieldData;
}
/// <summary>
/// 拼接所有的二进制数组为一个数组
/// </summary>
/// <param name="byteArrays">数组</param>
/// <returns></returns>
/// <remarks>加上结束边界</remarks>
public byte[] JoinBytes(ArrayList byteArrays)
{
int length = 0;
int readLength = 0;

// 加上结束边界
string endBoundary = Boundary + "--\r\n"; //结束边界
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
byteArrays.Add(endBoundaryBytes);

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

// 遍历复制
//
foreach(byte[] b in byteArrays)
{
b.CopyTo(bytes, readLength);
readLength += b.Length;
}

return bytes;
}
/// <summary>
/// 上传数据
/// </summary>
/// <param name="uploadUrl"></param>
/// <param name="bytes"></param>
/// <param name="responseBytes"></param>
/// <returns>上传是否成功</returns>
public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", ContentType);

try
{
responseBytes = webClient.UploadData(uploadUrl, bytes);
return true;
}
catch (WebException ex)
{
Stream resp = ex.Response.GetResponseStream();
responseBytes = new byte[ex.Response.ContentLength];
resp.Read(responseBytes, 0, responseBytes.Length);
}
return false;
}

}
}
redfox105 2006-09-15
  • 打赏
  • 举报
回复
可以模拟桌面PC浏览器,
自己构造一个请求,模拟浏览器把附件发到服务器去
xwy1982 2006-06-26
  • 打赏
  • 举报
回复
up
flysky913 2006-06-07
  • 打赏
  • 举报
回复
是的啊,我也正为这个问题郁闷啊。高手过来指点一下!
qwx99 2006-06-06
  • 打赏
  • 举报
回复
大家帮忙顶一下
qwx99 2006-06-05
  • 打赏
  • 举报
回复
各位前辈大侠 帮帮忙!!
lengyubing_1983 2006-06-05
  • 打赏
  • 举报
回复
我也想学习一下
shixin1198 2006-06-05
  • 打赏
  • 举报
回复
没做过 帮你顶 算了 我也想学习!
qwx99 2006-06-05
  • 打赏
  • 举报
回复
高手来看看

7,657

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧