What is the best way to upload a file to a server while working with Vue.js in the Filemanager plugin, or how can I access a global function

How can I upload a file to the server using Vue.js in the Filemanager plugin?

I have tried multiple methods and the console log shows that the upload was successful, but I am not able to see any files on the server. Does anyone know what could be wrong?

<template>
  <div class="FileManagement" id="file-manager" name="file-manager">
    <va-inner-loading :loading="isLoading">
      <div class="row">
        <div class="flex xs12 md12">
          <DxFileManager
            ref="fileManager"
            name="file"
            :file-system-provider="fileSystemProvider"
            current-path="Widescreen"
          >
            <DxPermissions
              :create="true"
              :copy="false"
              :move="false"
              :delete="true"
              :rename="false"
              :upload="true"
              :download="true"
            />
          </DxFileManager>
        </div>
      </div>
    </va-inner-loading>
  </div>
</template>
data() {
    return {
      isLoading: false,
      popupVisible: false,
      imageItemToDisplay: {},
      result: [],
      fileSystemProvider: new CustomFileSystemProvider({
        getItems,
        createDirectory,
        uploadFileChunk,
        onCurrentDirectoryChanged,
        downloadItems,
        renameItem,
        deleteItem,
      }),
    };
  },
  mounted() {
    this.initEvent();
  },
  methods: {
}

function uploadFileChunk(fileData, uploadInfo, destinationDirectory) {
  let self = this;
let formData = new FormData();
  formData.append("name", fileData.name);
  formData.append("file", fileData);

  let config = {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  };

  axios.post("/files/upload", formData, config).then(function (response) {
    if (response.status === 200) {
      console.log(response.data);
    }
  });
}

In my Global.vue file, I have a function. How can I call this function in my FileManager.vue file outside of the methods() section?

Global.vue
onUploadFile: function (
//code
) 

FileManager.vue
mounted() {},
methods: {},
function uploadFileChunk(fileData, uploadInfo, destinationDirectory) {
 // how can call global onUploadFile: function  ???
}

General

Request URL: Request Method: GET Status Code: 200 OK Remote Address: 192.168.1.120:80 Referrer Policy: strict-origin-when-cross-origin


Or should I use JavaScript methods to upload the file?

====================================================================

To RuNpiXelruN, I followed your suggestions to modify my code, and it showed as successful, but the server still did not receive the file.

General

Request URL: Request Method: POST Status Code: 301 Moved Permanently Remote Address: 192.168.67.164:80 Referrer Policy: strict-origin-when-cross-origin

Answer №1

@James just an FYI, make sure to follow the steps mentioned below:

formData.append("file", fileData, fileData.name)

If that doesn't work, consider updating the header to what's shown below and check if it resolves the issue.

'Content-Type': 'application/x-www-form-urlencoded'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message from Angular development server: Channel is reporting an error in handling the response. The UNK/SW_UNREACHABLE options

After recently installing a new Angular app, I encountered an issue while running 'ng serve'. The application initially loads without any problems, but after a few seconds, I started seeing a strange error in the console. Channel: Error in handle ...

Retrieve the id of an element from a JSON object

Dealing with quotes, single or double, can sometimes pose a challenge. I am working on a JavaScript function that sends data to a PHP file and receives a JSON response. Everything seems to be functioning correctly, except for the part where I need to out ...

Tips for composing a single aggregation within a query

In my query, I wanted to find the first record with a CREATE_DATE greater than or equal to a specific date and less than another date. After calculating the duration between these dates, I needed to write another query. However, I was unsure how to combine ...

Why does the ng-click function fail to execute when using the onclick attribute in AngularJS?

Whenever I try to invoke the ng-click function using onClick, I encounter an issue where the ng-click function is not being called. However, in my scenario, the model does open with the onClick function. //Function in Controller $scope.editProductDetail ...

What methods can Cypress use to validate content containing hyperlinks?

My current task is to develop an automation test that confirms the presence/display of content containing a hyperlink embedded within text. Please refer to the screenshot I have provided for better understanding, as it illustrates the specific content encl ...

Protractor functions perfectly on localhost, but encounters a Timeout error when used remotely - the asynchronous callback was not executed within the specified timeout period

Running protractor protractor.conf.js --baseUrl=http://localhost:4200/ executes successfully by filling data, validating elements, etc. However, when attempting to test the same website through a remote URL protractor protractor.conf.js --baseUrl=http://t ...

Utilize MaterialUI's Shadows Type by importing it into your project

In our project, we're using Typescript which is quite particular about the use of any. There's a line of code that goes like this: const shadowArray: any = Array(25).fill('none') which I think was taken from StackOverflow. Everything s ...

Leveraging ForEach to merge two arrays and generate a fresh entity

I am in search of my final result which should be as follows: result = [{x: '12/12', y: 90 }, {x: '12/11', y: 0}, {x: '12/10', y: 92}, {x: '12/9', y: 0}, ... ] Currently, I have two arrays to work with. The first a ...

Error alert: The system could not locate Google when trying to drop pins

Every time I attempt to place pins on the map, I encounter the "google is not defined" error. The map itself displays without any issues until I add the lines following the initMap() function. I have come across similar posts but none of the suggested so ...

How can I retrieve data from local storage based on a specific key value using a query?

In order to optimize the storage of data for my app, I have successfully stored a large amount in local storage. Now, I am faced with the task of fetching data in an array but only selecting key/values that are specifically chosen, rather than all or jus ...

What is the best way to display JSON data in a Listview control?

What is the best way to display JSON data in a list format? Currently, I am able to retrieve JSON data and display it in an alert dialog. The JSON data looks like this: [{"_id":"5449f20d88da65bb79a006e1","name":"name3","phone":"888888","service":"service ...

Tips for preventing HTML ID clashes while integrating with the Document Object Model of external websites

When incorporating additional HTML elements into a webpage using Javascript or jQuery, along with external CSS declarations, it is important to avoid conflicts with existing IDs and class names already present on the page. This could lead to issues if ther ...

Discover the art of highlighting errors with React-hook-form and MUI React

My code consists of the following component: const App = () => { const formProps = useForm({ mode: "onBlur", }); const { handleSubmit, formState, register, watch, reset } = formProps; return ( <FormProvider {...formProps}> & ...

Exploring the functions of JointJS within a Node.js environment

I am a beginner in using JavaScript, JointJS, and Node.js. I am currently working on a Node.js project in WebStorm, and the file structure looks like this: /test /bin www /node_modules /public /routes index.js users.js /views error.jade ...

Spinning image on button click with seamless animation in Javascript

I'm trying to make an image rotate every second using the code below, but it's not working. Can you help me figure out why? <html> <head> <style> .rotated-image { -webkit-transform: rotate(2deg); transform: rotate(2deg); } & ...

Creating a compact array from a larger array in JavaScript

I am currently using the jquery.bracket library and I am looking to split a large array into pairs like ["'Team 1', 'Team 2'"],["'Team 3', 'Team 4'"] from var all= ["'Team 1', 'Team 2'","'T ...

Can you use an ajax post to search for a boolean in MongoDB without converting on the server side?

I am facing an issue with my mongo collection that has documents containing a boolean field: { name : "Tom", active : true } { name : "Jerry", active : false } In my application's client side, there is an element that triggers an AJAX po ...

Choose a country from a modal in a React application

click here for image description I need help implementing the screenshot above. My goal is to change the default country name and flag when a user clicks on the country dropdown menu. I have been using headless UI so far, but I'm struggling to figure ...

Transforming an unordered list to function similarly to a select input

I am looking to style a ul list to function as a select form element. I have successfully populated a hidden input using my code. Now, I am trying to make the ul behave like a select input when either the keyboard or mouse is used. Previously, I had some ...

The validation for Australian mobile numbers should include the option to have spaces between the digits

How can I validate a mobile number properly? The first text input should start with 04 It should have a total of 10 digits, including 04 (e.g. 0412345678) Below is my input field: <form name="uploadForm"> <input type="tel" name="MobileNumber" ...