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

  • 相关阅读:
    栅格系统
    jq中文档的操作及正则的应用
    jq的简单操作
    jquery基础
    DOM&BOM
    布局和js的轮播图
    js中的事件
    js的高级操作
    js对页面的一些简单的操作
    第二章 ELKstack部署及配置
  • 原文地址:https://www.cnblogs.com/lucoo/p/4482611.html
Copyright © 2020-2023  润新知