如何在我的Web服务器上传递base64字符串?

时间:2016-09-12 09:32:46

标签: java c# android asp.net-web-api base64

我的代码是否正确?我已经在我的Web服务器上发布了这个。会发生什么,它正在创建一个文本文件,但base64字符串不会写在该文本文件上。

这些是我在Android Studio中的代码

    private void uploadImage() {
    final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
            new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
                   loading.dismiss();
                    Toast.makeText(MainActivity.this, s , Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

                    loading.dismiss();

                    Toast.makeText(MainActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            String image = getStringImage(bitmap);


            Map<String,String> params = new Hashtable<String, String>();

            params.put("b64", image);
            Log.d("base64: ", String.valueOf(params));

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(stringRequest);
}

这些是我在VS上的代码

    [HttpPost]
    public String  ProcessImg([FromBody] string b64)
    {
        String base64 = b64;
        String jsonStr = null;
        //function to create image from b64 string
        try
        {
            var FilePath = ConfigurationManager.AppSettings["imgFilePath"];

            if (!Directory.Exists(FilePath))
            {
                Directory.CreateDirectory(FilePath);
            }
           //to create file and write base64 string
            var name = DateTime.Now.ToString("MMddyyyy-HHmmss");
            var FileName = Path.Combine(FilePath, name + ".png");

            string path = Path.Combine(FilePath, name + ".txt");

            StreamWriter file = new StreamWriter(path);
            file.Write(base64);
            file.Close();

            if (File.Exists(FileName))
            {
                jsonStr = "file successfully created on server. :" + FileName;

            }
            else
            {
                jsonStr = "Sorry the file you tried to convert failed.";
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
            jsonStr = ex.Message;
        }

        //Algo

        return jsonStr;
    }

1 个答案:

答案 0 :(得分:0)

StreamWriter最适合using关键字,因为它实现IDisposable,会自动调用Dispose()方法,如下所示:

using(StreamWriter file = new StreamWriter())
{
    file.Write(base64);
}

否则,在关闭文件之前,您必须手动调用Flush()方法将缓冲的输入写入磁盘:

StreamWriter file = new StreamWriter();
file.Write(base64);
file.Flush();
file.Close();

在调用流的Dispose()方法时完成刷新部分,因此使用using关键字实现它会自动处理。