• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

xutils上传文件C#http服务ashx接收

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

android 端

protected void SaveImg()
{

String uploadHost="http://192.168.1.111:801/httpHandle/UploadHandler.ashx";
RequestParams params=new RequestParams();
params.addBodyParameter("action","addImg");
params.addBodyParameter("flie", new File(filepath));
uploadMethod(params,uploadHost);


}
public void uploadMethod(final RequestParams params,final String uploadHost) {
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.POST, uploadHost, params,new RequestCallBack<String>() {
@Override
public void onStart() {
// msgTextview.setText("conn...");
}
@Override
public void onLoading(long total, long current,boolean isUploading) {
if (isUploading) {
// msgTextview.setText("upload: " + current + "/"+ total);
} else {
// msgTextview.setText("reply: " + current + "/"+ total);
}
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// msgTextview.setText("reply: " + responseInfo.result);
showToask("上传成功");
}
@Override
public void onFailure(HttpException error, String msg) {
// msgTextview.setText(error.getExceptionCode() + ":" + msg);
showToask(msg);
}
});
}

 

C# 服务端

public void ProcessRequest(HttpContext context)
{

context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string action = context.Request["action"];

switch (action)
{

case "addImg"://上传文件
#region
bool IsValid = true;//检查只能为图片,仅小于100k可以上传
string messge = "";
string[] Types = { ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".ico" };
int lenth = 1024 * 500;//一百k
if (context.Request.Files.Count > 0)
{

for (int i = 0; i < context.Request.Files.Count; i++)
{
HttpPostedFile hpFile = context.Request.Files[i];
if (!String.IsNullOrEmpty(hpFile.FileName))
{
string ext = System.IO.Path.GetExtension(hpFile.FileName);
if (!Types.Contains(ext.ToLower()))
{
IsValid = false;
messge = "只能为图片类型";
break;

}
else if (hpFile.ContentLength > lenth)
{
IsValid = false;
messge = "文件不能大于" + lenth / 1024 + "kB";
break;
}

}
}

}
if (context.Request.Files.Count > 0 && IsValid)
{

for (int i = 0; i < context.Request.Files.Count; i++)
{


HttpPostedFile hpFile = context.Request.Files[i];
if (!String.IsNullOrEmpty(hpFile.FileName))
{
string ext = System.IO.Path.GetExtension(hpFile.FileName);
if (hpFile.ContentType != "image/jpeg" || hpFile.ContentType != "image/pjpeg")
{
//给文件取随及名
Random ran = new Random();
int RandKey = ran.Next(100, 999);
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + RandKey + ext;
//保存文件
string uriString = System.Web.HttpContext.Current.Server.MapPath("~/Upload/").ToString();
hpFile.SaveAs(uriString + fileName);
//提示上传成功
messge = "上传成功";
}
}
}
}

context.Response.Write(messge);
context.Response.End();

#endregion
break;
}
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#进阶之WebAPI(二)发布时间:2022-07-14
下一篇:
为什么C++函数形参默认值从最末一个赋值?发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap