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

  • 相关阅读:
    linux hosts文件详+mac主机名被莫名其妙修改
    WPF整理--动态绑定到Logical Resource
    WPF整理-使用逻辑资源
    WPF整理-自定义一个扩展标记(custom markup extension)
    WPF整理-XAML访问静态属性
    WPF整理-为控件添加自定义附加属性
    WPF整理-为User Control添加依赖属性
    使用MS Test进行单元测试
    WPF整理-XAML构建后台类对象
    毕业那点事儿--回顾在大学这7年
  • 原文地址:https://www.cnblogs.com/lucoo/p/4482611.html
Copyright © 2020-2023  润新知