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