Android xUtils 上传文件到C# webservice 服务器

12 篇文章 0 订阅
RequestParams params = new RequestParams("http://192.168.1.160:911/WebService1.asmx/HelloWorld");
		// 有上传文件时使用multipart表单, 否则上传原始文件流.
		params.setMultipart(true);
		params.addBodyParameter("file", new File(AppConfig.DATABASE_PATH + AppConfig.DATABASE_NAME));
		x.http().post(params, new CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException arg0) {
				// TODO Auto-generated method stub
				LogUtil.e("onCancelled");
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {
				// TODO Auto-generated method stub
				LogUtil.e("onError");
			}

			@Override
			public void onFinished() {
				// TODO Auto-generated method stub
				LogUtil.e("onFinished");
			}

			@Override
			public void onSuccess(String arg0) {
				// TODO Auto-generated method stub
				LogUtil.e(arg0);
			}
		});

第一种接受方式:

[WebMethod]
        public void HelloWorld()
        {
           // System.IO.Stream stream = HttpContext.Current.Request.InputStream;//这是获得的流
            
            var httpFileCollection = HttpContext.Current.Request.Files;
            if (httpFileCollection.Keys.Count > 0)
            {
                HttpPostedFile httpPostedFile = httpFileCollection[0];
                httpPostedFile.SaveAs(Server.MapPath(httpPostedFile.FileName));
                Context.Response.Write(httpPostedFile.FileName);
                Context.Response.End(); 
            }
            
        }

第二种接受方式:

[WebMethod]
        public void HelloWorld()
        {
            System.IO.Stream data = HttpContext.Current.Request.InputStream;//这是获得的流
            byte[] buffer = new byte[data.Length];
            System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("aa.db"), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            fs.Write(buffer, 0, buffer.Length);
            fs.Flush();
            fs.Close();
            
        }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值