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

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;
}
}

 

转载于:https://www.cnblogs.com/lucoo/p/4482611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值