使用SPservices将文件上传到Sharepoint列表

时间:2018-10-10 14:13:43

标签: javascript jquery sharepoint-2013 spservices

我在尝试从文件选择器上传附件时遇到问题。当我添加要上传的文件时,我只会收到错误消息。

无法获取未定义或空引用的属性“ 0”

,它指向行var file = $("#SupportDoc").files[0];

这是我的代码。任何帮助都会很棒。

<div class="form-group" style="margin:10px;">
    <label for="exampleFormControlFile1" style="border:none;">Supporting Documentation</label>
    <input style="border:none;" type="file" class="form-control-file" id="SupportDoc">
</div>

-

function attach(){
var file = $("#SupportDoc").files[0];

getFileBuffer(file).then(function(buffer) {
  var binary = "";
  var bytes = new Uint8Array(buffer);
  var i = bytes.byteLength;
  while (i--) {
    binary = String.fromCharCode(bytes[i]) + binary;
  }
  $().SPServices({
    operation: "AddAttachment",
    listName: "Tasks",
    listItemID: taskID,
    fileName: file.name,
    attachment: btoa(binary)
  });
});
}

1 个答案:

答案 0 :(得分:0)

为此替换行

public class MapFragment extends Fragment implements OnMapReadyCallback
{

    private static final String TAG = "MapFragment";

    private Boolean LocationPermissionsGranted = false;  //boolean for location method
    private GoogleMap Map;
    private static final int ERROR_REQUEST = 69; //error code relates to isservicesOK method
    private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    private static final int LOCATION_PERMISSION_CODE = 1234;

    public MapFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        //TODO: Instead of hardcoding the title perhaps take the user name from somewhere?
        // Note the use of getActivity() to reference the Activity holding this fragment
        getActivity().setTitle( "YOUR CLINICS" );
        isServicesOK();
        getLocationPermission();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ButterKnife.bind( getActivity() );

        return inflater.inflate( R.layout.fragment_map, container, false );


    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        Toast.makeText(getActivity(), "Maps is ready", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Map is ready");
        Map = googleMap;
    }
    private void initMap() {
        Log.d(TAG, "initializing map...");
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); //ignore warning

        mapFragment.getMapAsync(MapFragment.this);
    }

    private void getLocationPermission()
    {
        Log.d(TAG, "Retrieving location permissions");
        String[] permissions = {android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION};

        if (ContextCompat.checkSelfPermission(this.getActivity().getApplicationContext(), //ignore
                FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this.getActivity().getApplicationContext(),
                    COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                LocationPermissionsGranted = true;
            } else {
               requestPermissions(permissions,
                        LOCATION_PERMISSION_CODE);
            }
        } else {
            requestPermissions(permissions,
                    LOCATION_PERMISSION_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        Log.d(TAG, "onRequestPermissionsResult: called.");
        LocationPermissionsGranted = false;

        switch (requestCode)
        {
            case LOCATION_PERMISSION_CODE:
            {
                if (grantResults.length > 0)
                {
                    for (int i = 0; i < grantResults.length; i++) //ignore
                    {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED)
                        {
                            LocationPermissionsGranted = false;
                            Log.d(TAG, "onRequestPermissionsResult: permission failed");
                            return;
                        }
                    }
                    Log.d(TAG, "onRequestPermissionsResult: permission granted");
                    LocationPermissionsGranted = true;
                    //if permissions are granted we initialize the map
                    initMap();
                }
            }
        }
    }


    // integrate this method with the start of the application eg after a button has been clicked
    // Debugging method- checks if google services is available/functional
    public boolean isServicesOK ()
    {
        Log.d(TAG, "isServicesOK: checking if your Google Services is up to date"); //debugging

        int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getActivity()); /*//log d = debug*/

        if (available == ConnectionResult.SUCCESS)
        {
            /*//checks if everything is fine*/
            Log.d(TAG, "isServicesOK: Google Play Services is functional");
            return true;
        }
        else if (GoogleApiAvailability.getInstance().isUserResolvableError(available))
        {
            /* //If an error occurs, and it is resolvable*/
            Log.d(TAG, "isServicesOK: A fixable error has occured!");
            Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(getActivity(), available, ERROR_REQUEST);
            dialog.show();  /*//This prompts google to provide a error message and solution when a fixable error has occurred*/
        }

        else
        {
            Toast.makeText(getActivity(), "Google Play Services is dysfunctional, sorry!", Toast.LENGTH_SHORT).show(); /*//if it cant be fixed for whatever reason lmao*/
        }
        return false;
    }

}

var file = document.getElementById("SupportDoc").files[0];