0

嗨,我尝试将文件上传到我的 backendless rest api..但我无法取得好的结果。看来我无法读取文件。代码有什么问题?

Logcat 上的错误响应

02-25 21:03:02.138 31815-31815/com.bloxofcode.backendless I/Error: Server reported an error - cannot read the file.

这是示例代码..

public void OnClick_Upload(View v)  {
        // create a file locally so there is something to upload

        String filename = "myhelloworld-async.txt";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(filename);
            fileOutputStream.write("Hello mbaas!\nUploading files is easy!".getBytes());
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        final File file = new File(filename);

        Log.i("FileStream",file.getPath());
        // now upload the file
        backend.Files.upload( file, "/media", new AsyncCallback<BackendlessFile>()
        {
            @Override
            public void handleResponse( BackendlessFile uploadedFile )
            {
                Log.i("Output","File has been uploaded. File URL is - " + uploadedFile.getFileURL());
                //System.out.println( "File has been uploaded. File URL is - " + uploadedFile.getFileURL() );
                //file.delete();
            }

            @Override
            public void handleFault( BackendlessFault backendlessFault )
            {
                Log.i("Error","Server reported an error - " + backendlessFault.getMessage() );
                //System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
            }
        } );
    }
4

1 回答 1

0

看起来您创建的文件无法读取,甚至无法上传文件。

我认为这对你会更好:

byte[] bytes = "Hello mbaas!\nUploading files is easy!".getBytes(); 

Backendless.Files.saveFile( "tempFolder", "myhelloworld-async.txt", bytes, new AsyncCallback<BackendlessFile>()
{
  @Override
  public void handleResponse( BackendlessFile uploadedFile )
  {
       Log.i("Output","File has been uploaded. File URL is - " + uploadedFile.getFileURL());
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i("Error","Server reported an error - " + backendlessFault.getMessage() );
  }
} );
于 2016-02-26T14:25:28.890 回答