Loopj - 使用RequestParams c#.net上传文件

时间:2016-02-04 09:07:24

标签: android asp.net-web-api httpclient loopj

尝试使用loopj上传带有params的文件。 我试图从Request.Form中获取Request.Files和params的文件["创建"] 但它没有上传到服务器。

Android Post方法

try { 
                String createTeamURL = "http://url"; 
                RequestParams params = new RequestParams();
                params.put("file", new File(pathoffile));
                params.add("create", regString);
                AsyncHttpClient client = new AsyncHttpClient();
                client.post(createTeamURL, params, new AsyncHttpResponseHandler() {

                    @Override
                    public void onStart() {
                        // called before request is started
                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                        // called when response HTTP status is "200 OK"
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                        // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                    }

                    @Override
                    public void onRetry(int retryNo) {
                        // called when request is retried
                    }
                });
            } catch (Exception e) {
                Log.e("createTeamPreStep", e.getMessage());
            }

我的Web Api c#方法

 [HttpPost]
        public async Task<string> CreateUHS()
        {
            var resultString = "";

            foreach(HttpPostedFileBase s in Request.Files)
            {
                var a=s; 
            }

            String sdf = Request.Form["create"];

        }

1 个答案:

答案 0 :(得分:0)

你需要使用put for string args。 请在下面找到服务器和客户端方法。 还有一件事我真的担心你的命名变量。这不好。请改变它。快乐的编码。

    String createTeamURL = "http://url"; 
            RequestParams params = new RequestParams();
            params.put("file", new File(pathoffile));
            params.put("create", regString);

服务器(Web api)

 [HttpPost]
        public async Task<string> CreateUHS()
        {
            var file=Request.Files[0];
            String otherArg = Request.Form["create"];
        }