繁体   English   中英

从C#Monogame Android中的服务器下载文件

[英]Download files from a server in C# monogame android

我的APK非常大,使用的是高分辨率文件,我想知道是否可以调用一种方法来下载这些文件并将其移至程序文件中。 我可以将文件上传到服务器,以便在应用程序首次启动时将它们下载并放置在正确的文件夹中。 有谁知道该怎么做?

不能确定C#是诚实的,但仅作为示例。 我用以下方式下载图像:

        String URL = new String(Globals.getServerURL()+"/getcompanylogo.asp");
        File myExternalFile = new File(cxt.getExternalFilesDir(filepath), fileName);
        ByteArrayOutputStream output=null;
          if(Globals.CheckInternet()=="FOUND" && !Globals.getServerURL().isEmpty()){
                try {
                    URL url = new URL(URL);
                    URLConnection urlConnection = url.openConnection();
                    urlConnection.setReadTimeout(5000);
                    urlConnection.setConnectTimeout(5000);

                    InputStream is = new BufferedInputStream(urlConnection.getInputStream());

                    byte[] buffer = new byte[8192];
                    int bytesRead;
                    output = new ByteArrayOutputStream();

                    while ((bytesRead = is.read(buffer)) != -1) {
                        output.write(buffer, 0, bytesRead);
                    }

                    FileOutputStream fos = new FileOutputStream(myExternalFile);
                    fos.write(output.toByteArray());
                    fos.flush();
                    fos.close();
                    return "OK";
                    //bmp = createBitmap();
                    //return output.toByteArray();
                } catch (MalformedURLException e) {
                    return "BadUrl";
                }    catch (ConnectTimeoutException e) {
                    return "ConnectionTimeout";
                }    catch (ConnectException e) {
                    return "ConnectionException";
                } catch (IOException e) {
                    return "readwriteexception";
                }
            }

提供图像的ASP页面包含;

<%Response.CacheControl = "no_cache" %>
<%Response.AddHeader "Pragma", "no_cache" %>
<%Response.Expires = -1 %>

<%

 pathcode = (session("webpath")+"\companylogo.png")
response.buffer = true

'create a stream object
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")


 objStream.Type = 1
 objStream.Open
 objStream.LoadFromFile pathcode
 Response.ContentType = "image/PNG"
 Response.AddHeader "content-disposition", "inline;filename=image.png"
 Response.BinaryWrite objStream.Read

'clean up..
objStream.Close
Set objStream = Nothing

 %> 

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM