File uploading method, device, equipment and computer readable medium

By combining depth-first and breadth-first strategies in recursive programs to monitor and manage the call stack memory, the problem of call stack overflow caused by large file levels and quantities is solved, and the smooth upload of files is achieved.

CN114265818BActive Publication Date: 2024-10-11BEIJING PERFECT WORLD SOFTWARE TECH DEV CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111619062.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-27
Publication Date
2024-10-11
Estimated Expiration
2041-12-27

AI Technical Summary

Technical Problem

The increasing level and number of files causes the call stack to overflow when the recursive program reads files, making it impossible to upload files smoothly. Existing technology fails to provide an effective solution.

Method used

Use the depth-first strategy to read file data, and switch to the breadth-first strategy when the call stack memory usage reaches the warning value, clear the context environment record access path, generate a directory tree, and push the file data into the upload queue according to the directory structure for upload.

Benefits of technology

It effectively avoids call stack overflow, ensures that files can be read and uploaded smoothly even when the file level and quantity are large, and solves the memory overflow problem when recursive programs read files.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114265818B_ABST
    Figure CN114265818B_ABST
Patent Text Reader

Abstract

The present application relates to a file uploading method, device, equipment and computer-readable medium. The method includes: reading the file data to be uploaded based on a depth-first strategy, and monitoring the memory status of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy; when it is monitored that the memory occupancy rate of the call stack reaches the warning value, the context environment record access path stored in the call stack is cleared, and the file data is read based on the breadth-first strategy to obtain a directory tree of the file data; the file data is pushed into the upload queue according to the directory structure of the directory tree for file upload. The above scheme solves the technical problem that the call stack overflows when the recursive program reads the file due to the excessive number and level of the file, and the file cannot be uploaded smoothly.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of Internet technology, and in particular to a file uploading method, device, equipment and computer-readable medium. Background Art

[0002] In a recursive program, each level of recursion must add a context to the call stack, so if the program has infinite recursion (or simply too many levels of recursion), the call stack will overflow.

[0003] File upload refers to the process of reading files through recursive programs and transferring them from one terminal to another or to the server. With the advent of the information age, data interaction and sharing are becoming increasingly close, and a problem that comes with it is that the number and level of files are increasing, which will further cause the call stack memory to continue to increase until the call stack memory overflows, causing thread operation timeouts, and even file loss in severe cases.

[0004] At present, in the relevant technology, there is a problem that the hierarchy and number of files are getting larger and larger, resulting in call stack memory overflow when the recursive program reads the file and the file cannot be uploaded smoothly. One way is to strictly control the file hierarchy and the number of files to ensure that the stack space required for the entire recursion remains within the design threshold. Another way is to continuously expand the stack space. However, the doubling of cost and size is difficult for users to accept.

[0005] There is currently no effective solution to the problem that the number and level of files are increasing, which causes the call stack memory to overflow when the recursive program reads the files and makes it impossible to upload the files smoothly. Summary of the invention

[0006] The present application provides a file uploading method, apparatus, device and computer-readable medium to solve the technical problem that the level and number of files are increasing, resulting in a call stack overflow when a recursive program reads files and the file cannot be uploaded smoothly.

[0007] According to one aspect of an embodiment of the present application, the present application provides a file uploading method, including:

[0008] Read the file data to be uploaded based on the depth-first strategy, and monitor the memory state of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy;

[0009] When it is detected that the memory usage of the call stack reaches a warning value, the context environment record access path stored in the call stack is cleared, and the file data is read based on a breadth-first strategy to obtain a directory tree of the file data; and,

[0010] According to the directory structure of the directory tree, the file data is pushed into the upload queue for file upload.

[0011] Optionally, before reading the file data to be uploaded based on the depth-first strategy, the method further includes:

[0012] Sending preset condition parameters to the service end to instruct the service end to prepare the file data to be uploaded according to the preset condition parameters, wherein the preset condition parameters include a file level threshold and a file quantity threshold;

[0013] When the actual file level of the file data fed back by the business end is within the file level threshold range and the actual file number of the file data is within the file number threshold range, the file data to be uploaded is read based on the depth-first strategy.

[0014] Optionally, reading the file data to be uploaded based on the depth-first strategy includes:

[0015] The root folder is used as the reading starting point. Whenever the next level folder is read, a context environment is generated to record the access path to record the current layer position.

[0016] The context environment record access path is stored in the call stack, and the next level folder is entered to continue reading file data;

[0017] When there is no next-level folder in the current-level folder, the previous-level folder of the current-level folder is returned by recording the access path in the context environment in the call stack;

[0018] When the access path is recorded through the context environment in the call stack back to the root folder and all individual files in the root folder are read, a directory tree is generated with the root folder as the root node, subfolders at each level as child nodes, and individual files at each level as leaf nodes.

[0019] Optionally, switching to a breadth-first strategy to read file data includes:

[0020] When the call stack memory usage reaches the warning value, the file level and number of files read based on the depth-first strategy are updated to new preset condition parameters, and a reading queue is created;

[0021] Push the root folder into the read queue as the reading starting point;

[0022] Push the folder at the head of the read queue to read a single file in the current folder, and push the folder below the current folder into the read queue;

[0023] When all individual files in the current folder have been read and all lower-level folders have been pushed into the read queue, the folder at the head of the queue is pushed out to continue reading the file data in each folder in the order in which they were queued;

[0024] When all folders are pushed out of the read queue, a directory tree is generated with the root folder as the root node, folders at each level as child nodes, and individual files at each level as leaf nodes.

[0025] Optionally, reading file data includes:

[0026] Generate a corresponding microtask for each folder or each single file, and push each microtask into the microtask queue in the order of generation;

[0027] When the main thread is in an idle state, the main thread is called to execute the microtask queue and the main thread's occupied time is recorded, wherein the main thread's occupied time is accumulated when it continuously executes microtasks;

[0028] When the occupied time is greater than the preset time threshold, corresponding macro tasks are created for the remaining microtasks in the microtask queue, so that the main thread executes a macro task each time after executing a microtask, wherein the occupied time is reset after the main thread executes the macro task.

[0029] Optionally, pushing the file data into the upload queue for file uploading according to the directory structure of the directory tree includes:

[0030] Determine a fragment size of the first file according to the file type of the first file in the file data, and fragment the first file according to the fragment size to obtain a plurality of first file blocks, wherein the size of the first file exceeds a first threshold;

[0031] Pushing out the first file blocks one by one from the upload queue for uploading, and calculating the checksum of each first file block;

[0032] When the last first file block is pushed out of the upload queue for uploading, merging the check codes of all first file blocks to obtain a complete check code of the first file; and

[0033] Upload the complete verification code.

[0034] Optionally, pushing the file data into the upload queue for file uploading according to the directory structure of the directory tree further includes:

[0035] Determine a file type of the second file, and call a first sharding strategy that matches the file type to shard the second file to obtain a plurality of second file blocks;

[0036] Selecting a target channel from the channel pool to upload multiple second file blocks, wherein the target channel is a channel in the channel pool that has not been selected within a preset time;

[0037] During the upload of the plurality of second file blocks, adjusting the first sharding strategy according to the upload status of the uploaded second file blocks to obtain a second sharding strategy;

[0038] The second fragmentation strategy is used to re-segment the remaining second file blocks that have not been uploaded, and a target channel is reselected for uploading.

[0039] According to another aspect of the embodiment of the present application, the present application provides a file uploading device, including:

[0040] A first reading module is used to read the file data to be uploaded based on a depth-first strategy, and monitor the memory state of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy;

[0041] The second reading module is used to clear the context environment record access path stored in the call stack when it is monitored that the memory usage of the call stack reaches a warning value, and read the file data based on the breadth-first strategy to obtain a directory tree of the file data;

[0042] The upload module is used to push file data into the upload queue for file upload according to the directory structure of the directory tree.

[0043] According to another aspect of an embodiment of the present application, the present application provides an electronic device, including a memory, a processor, a communication interface and a communication bus, wherein the memory stores a computer program that can be run on the processor, the memory and the processor communicate through the communication bus and the communication interface, and the steps of the above method are implemented when the processor executes the computer program.

[0044] According to another aspect of an embodiment of the present application, the present application also provides a computer-readable medium having a non-volatile program code executable by a processor, and the program code enables the processor to execute the above method.

[0045] The above technical solution provided by the embodiment of the present application has the following advantages compared with the related art:

[0046] The technical solution of the present application is to read the file data to be uploaded based on the depth-first strategy, and monitor the memory status of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy; when it is monitored that the memory occupancy rate of the call stack reaches the warning value, the context environment record access path stored in the call stack is cleared, and the file data is read based on the breadth-first strategy to obtain the directory tree of the file data; according to the directory structure of the directory tree, the file data is pushed into the upload queue for file upload. The present application uses dynamic algorithm planning. When the depth-first strategy reads too many file levels, resulting in the context environment record access path stored in the call stack greatly occupying the memory space of the call stack, the breadth-first strategy is used in a timely manner to release the context environment record access path stored in the call stack, and successfully read all files, solving the technical problem that the number and level of files are increasing, resulting in the overflow of the call stack when the recursive program reads the file, and the file cannot be uploaded smoothly. BRIEF DESCRIPTION OF THE DRAWINGS

[0047] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0048] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related technologies, the drawings required for use in the embodiments or the related technical descriptions are briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0049] Figure 1 A schematic diagram of a hardware environment for an optional file uploading method provided according to an embodiment of the present application;

[0050] Figure 2 A schematic diagram of an optional file uploading method flow according to an embodiment of the present application;

[0051] Figure 3 A block diagram of an optional file uploading device provided according to an embodiment of the present application;

[0052] Figure 4 A schematic diagram of an optional electronic device structure provided in an embodiment of the present application. DETAILED DESCRIPTION

[0053] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0054] In the subsequent description, the suffixes such as "module", "component" or "unit" used to represent elements are only used to facilitate the description of the present application and have no specific meaning. Therefore, "module" and "component" can be used interchangeably.

[0055] In the related technology, there is a problem that the hierarchy and number of files are getting larger and larger, which leads to call stack memory overflow when the recursive program reads the files. One way is to strictly control the file hierarchy and the number of files to ensure that the stack space required for the entire recursion remains within the design threshold. Another way is to continuously expand the stack space. However, the doubling of cost and size is difficult for users to accept.

[0056] In order to solve the problem mentioned in the background technology, according to one aspect of the embodiments of the present application, an embodiment of a file uploading method is provided.

[0057] Optionally, in the embodiment of the present application, the above file upload method can be applied to Figure 1 In the hardware environment composed of the terminal 101 and the server 103 shown in FIG. Figure 1 As shown, the server 103 is connected to the terminal 101 via a network, and can be used to provide services for the terminal or a client installed on the terminal (such as file reading, file segmentation, calculation of checksums, file upload, etc.), and a database 105 can be set on the server or independently of the server to provide data storage services for the server 103. The above-mentioned network includes but is not limited to: a wide area network, a metropolitan area network or a local area network, and the terminal 101 includes but is not limited to a PC, a mobile phone, a tablet computer, etc.

[0058] A file uploading method in the embodiment of the present application can be executed by the terminal 101, such as Figure 2 As shown, the method may include the following steps:

[0059] Step S202, read the file data to be uploaded based on the depth-first strategy, and monitor the memory status of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy.

[0060] The file upload method in this embodiment can be applied to the file upload scenario of transferring file data from one terminal to another terminal or server. The file data can be in the form of nested multi-level folders as containers. Each level of folders can accommodate file data. The file data may include text files, multimedia files, executable files, library files, system files, etc.

[0061] When there are few file levels and files, the depth-first strategy can be used to quickly traverse and read all files, because the depth-first strategy is based on easy-to-code and easy-to-understand recursive programs, and the time overhead is relatively small. The basic idea of ​​the depth-first strategy is that in the current layer, when a path is selected to search, it goes deeper along the current path until it can no longer go deeper. For each layer, the recursive program will generate a context environment and save it through the call stack. When the current path can no longer go deeper, that is, when it reaches the end of the path, the current path search ends and the memory is reclaimed; continue in the current layer and explore whether there are other paths. If there are other paths, continue to go deeper, otherwise until all branch paths are traversed and all files are read.

[0062] The current context environment is saved in the call stack, which takes up the memory space of the call stack. As the file level and the number of files continue to increase, the available memory space of the call stack continues to shrink when reading file data through the depth-first strategy. In order to prevent the stack from bursting, that is, to prevent the call stack from overflowing, it is necessary to monitor the memory status of the call stack in real time.

[0063] Step S204, when it is monitored that the memory occupancy rate of the call stack reaches a warning value, the context environment recorded access path stored in the call stack is cleared, and the file data is read based on the breadth-first strategy to obtain a directory tree of the file data.

[0064] In an embodiment of the present application, in order to prevent the stack from exploding, when it is monitored that the memory occupancy rate of the call stack reaches a warning value, the reading strategy can be switched to a breadth-first strategy. The context environment record access path stored in the call stack is first cleared, and then a queue is dynamically created to traverse the file data layer by layer starting from the root folder, and the traversed subfolders are pushed into the queue. Then, the file data in the subfolders are read one by one in the order of entry into the queue until all folders are traversed, the queue is cleared, and the reading of all file data is completed.

[0065] In the embodiment of the present application, a memory space of a preset size can be first applied to the memory for allocation, and the preset size is used as the initial queue length of the queue. If there are many file levels and folders, so that the queue length needs to be extended, a new memory space can be dynamically applied to the memory for allocation to increase the queue length. Therefore, the breadth-first strategy can be used for situations with many file levels, avoiding thread timeouts caused by call stack explosion, and can smoothly read all file data without losing files.

[0066] In an embodiment of the present application, the directory tree can be further expanded based on the breadth-first strategy on the basis of the directory tree obtained by the depth-first strategy, that is, the traversal still needs to start from the root folder, but when the traversal reaches a file or folder that has been read, the file or folder can be skipped.

[0067] In the embodiment of the present application, the file directory can also be displayed to the user according to the directory tree.

[0068] Step S206: Push the file data into the upload queue according to the directory structure of the directory tree for file upload.

[0069] Through the above steps S202 to S206, the present application adopts different reading strategies for different file nesting levels. When the depth-first strategy reads too many file levels, resulting in the context environment record access path stored in the call stack greatly occupying the memory space of the call stack, the breadth-first strategy is timely used to release the context environment record access path stored in the call stack and successfully read all files, thereby solving the technical problem that the number and level of files are increasing, resulting in the overflow of the call stack when the recursive program reads the file and the inability to upload the file smoothly.

[0070] Optionally, before reading the file data to be uploaded based on the depth-first strategy, the method further includes:

[0071] Step 1: Sending preset condition parameters to the service end to instruct the service end to prepare the file data to be uploaded according to the preset condition parameters, wherein the preset condition parameters include a file level threshold and a file quantity threshold;

[0072] Step 2: When the actual file level of the file data fed back by the business end is within the file level threshold range and the actual file number of the file data is within the file number threshold range, the file data to be uploaded is read based on a depth-first strategy.

[0073] In an embodiment of the present application, conditional parameters can be pre-set based on the design parameters of the call stack. The conditional parameters can be a file level threshold and / or file quantity threshold that causes the call stack memory to overflow, or a file level threshold and / or file quantity threshold that causes the memory occupancy of the call stack to reach a warning value. The conditional parameters can also be set based on actual conditions and actual needs.

[0074] Before obtaining the file data to be uploaded, the preset condition parameter can be sent to the service end to instruct the service end to prepare the file data to be uploaded according to the preset condition parameter. In this way, if the file data to be uploaded prepared by the service end meets the preset condition parameter, the local end can perform a depth-first traversal of the file data through a recursive program obtained by simple coding.

[0075] If the file data to be uploaded produced by the business end exceeds the preset condition parameters, this end can read the file data based on the depth-first strategy within the preset condition parameters, and adopt the breadth-first strategy to read the file data for the part exceeding the preset condition parameters. Alternatively, if it is determined that the file data to be uploaded produced by the business end exceeds the preset condition parameters, this end can directly adopt the breadth-first strategy to read all file data.

[0076] In an embodiment of the present application, the preset condition parameter can be dynamically changed, such as being set according to the design parameters of the call stack for the first time, and then each time the file data is read, the preset condition parameter used for the next reading of the file data can be dynamically set according to the situation of the call stack. For example, if the current preset condition parameter is 200 layers and 10,000 files, and when the memory occupancy rate of the 150-layer call stack read based on the depth-first strategy has reached the warning value, the 150-layer call stack will be updated as the preset condition parameter sent to the business end next time. On the contrary, if the memory occupancy rate of the 250-layer call stack read based on the depth-first strategy is still at a low level, the preset condition parameter sent to the business end next time can be updated to 250 layers or even higher.

[0077] Optionally, this end can combine the memory space judgment of the call stack with the above-mentioned preset condition parameters. If the business end does not feedback the level and number of file data to be uploaded, this end can first read the file data according to the depth-first strategy; when the read file level and / or file number reaches the preset condition parameters but the memory occupancy of the call stack is still in the low occupancy interval, continue to read the file data based on the depth-first strategy until the memory occupancy of the call stack reaches the warning value, then switch to the breadth-first strategy to read the file data and update the preset condition parameters; when the read file level and / or file number reaches the preset condition parameters and the memory occupancy of the call stack is already in the high occupancy interval, switch to the breadth-first strategy to read the file data. Among them, the specific setting method of the low occupancy interval and the high occupancy interval can be set according to the actual situation or actual needs.

[0078] Optionally, the local end may also determine whether to switch to a breadth-first strategy to read files based only on the above-mentioned preset condition parameters. Specifically, when reading file data according to the depth-first strategy, the local end may record in real time the file level and number of files that have been read, and switch to a breadth-first strategy when the file level and / or the number of files reaches the above-mentioned preset condition parameters.

[0079] Optionally, reading the file data to be uploaded based on the depth-first strategy includes:

[0080] Step 1, taking the root folder as the reading starting point, whenever the next level folder is read, a context environment is generated to record the access path to record the current layer position;

[0081] Step 2: store the context environment record access path to the call stack, and enter the next level folder to continue reading file data;

[0082] Step 3: if there is no next-level folder in the current-level folder, return to the previous-level folder of the current-level folder by recording the access path in the context environment in the call stack;

[0083] Step 4, when the access path is recorded through the context environment in the call stack back to the root folder and all individual files in the root folder are read, a directory tree is generated with the root folder as the root node, subfolders of each level as child nodes, and individual files of each level as leaf nodes.

[0084] In the embodiment of the present application, the depth-first strategy for reading files is that when a path is selected for searching, the current path is followed until it cannot go any deeper. Each time a layer is deepened, the recursive program generates a context environment record access path to record the current layer position and save it in memory. When the current path cannot go any deeper, that is, when the end of the path is reached, the context environment record access path in the call stack is used to return to the previous layer and explore whether there are other paths. If there are other paths, continue to go deeper, otherwise continue to return to the next layer by calling the context environment record access path in the call stack until all branch paths are traversed and all files are read. Finally, a directory tree is created with the root folder as the root node, the subfolders of each level as child nodes, and the single files in each level as leaf nodes.

[0085] Optionally, switching to a breadth-first strategy to read file data includes:

[0086] Step 1: When the call stack memory usage reaches a warning value, the file level and number of files read based on the depth-first strategy are updated to new preset condition parameters, and a read queue is created;

[0087] Step 2: Push the root folder into the read queue as the reading starting point;

[0088] Step 3, push the folder at the head of the read queue to read a single file in the current folder, and push the folder at the next level of the current folder into the read queue;

[0089] Step 4: When all individual files in the current folder have been read and all lower-level folders have been pushed into the read queue, the folder at the head of the queue is pushed out to continue reading the file data in each folder in the order of entry into the queue;

[0090] Step 5, when all folders are pushed out of the read queue, a directory tree is generated with the root folder as the root node, folders at each level as child nodes, and single files at each level as leaf nodes.

[0091] In an embodiment of the present application, the breadth-first strategy is to first clear the context environment record access path stored in the call stack, then dynamically create a queue, traverse the file data layer by layer starting from the root folder, and push the traversed subfolders into the queue, and then read the file data in the subfolders one by one in the order of entering the queue, until all folders are traversed, the queue is cleared, and the reading of all file data is completed.

[0092] The present application also provides a file reading method to avoid continuous occupation of the main thread when the number of files is extremely large, such as hundreds of thousands or more. The following is an explanation using the JavaScript environment as an example.

[0093] Optionally, reading file data includes:

[0094] Step 1: Generate a corresponding microtask for each folder or each single file, and push each microtask into the microtask queue in the order of generation;

[0095] Step 2: When the main thread is in an idle state, call the main thread to execute the microtask queue and record the occupied time of the main thread, wherein the occupied time of the main thread when executing microtasks continuously is accumulated;

[0096] Step 3, when the occupied time is greater than the preset time threshold, corresponding macro tasks are created for the remaining microtasks in the microtask queue, so that the main thread executes a macro task every time after executing a microtask, wherein the occupied time after the main thread executes the macro task is reset.

[0097] One of the major features of the JavaScript language is single-threading, that is, only one thing can be done at the same time. In order to prevent the main thread from being blocked, the event loop solution came into being. The event loop is coordinated through the task queue mechanism. The core events of each loop include reading the tasks in the task queue for execution, then checking whether there is a microtask queue. If so, the microtasks in the microtask queue are executed until the microtask queue is cleared and the page is rendered. JavaScript can read events such as user interaction operations, script execution results, UI rendering results, and network processing results through rendering pages. Once the microtask queue is too long and the main thread is continuously occupied, the page cannot update other operation results in time, and the page is stuck, resulting in a very poor user experience.

[0098] In the embodiment of the present application, when reading file data, it is necessary to create a microtask for each folder or each single file. Once the number of files that need to be read is extremely large, directly using the microtask to read the execution file data reading task will cause the microtask queue to be extremely long, further causing the main thread to be continuously occupied by the microtask queue. Therefore, the present application adopts the technique of macrotask + microtask to process, that is, when using microtask to read each file or folder, a macrotask is created at the same time, and then these two tasks are combined into one macrotask, so that the microtask queue is effectively split into different macrotasks. The main thread executes a macrotask each time after executing a microtask. After executing the macrotask, the page will be rendered, thereby reading the user's interactive operations, script execution results, UI rendering results, and network processing results and other events in the rendering stage, effectively solving the problem of continuous occupation of the main thread.

[0099] In the embodiment of the present application, another method of combining microtasks + macrotasks to solve the problem of continuous occupation of the main thread is also provided. Specifically, the size of the number of files can also be judged according to the file directory that has been read, because the corresponding microtasks have been generated for each folder or each single file in the aforementioned steps, so the number of files can be judged according to the number of microtasks. When it is judged that the number of files is greater than the number threshold, corresponding macrotasks can be created for the remaining microtasks in the microtask queue respectively, without having to wait for the main thread's occupancy time to be greater than the preset time threshold to create corresponding macrotasks for the remaining microtasks in the microtask queue. Thus, the present application can further solve the problem of continuous occupation of the main thread, and eliminate the signs of continuous occupation of the main thread in the embryonic stage.

[0100] When uploading files, it is usually necessary to perform a file integrity check, which requires calculating the file checksum before uploading the file. When uploading files through a browser, once the data size of a single file is large (such as 1G), the browser may overflow the memory, causing the browser to crash directly. Therefore, this application also provides a file upload method that can avoid browser crashes, which is described in detail below.

[0101] Optionally, pushing the file data into the upload queue for file uploading according to the directory structure of the directory tree includes:

[0102] Step 1, determining a fragment size of the first file according to the file type of the first file in the file data, and fragmenting the first file according to the fragment size to obtain a plurality of first file blocks, wherein the size of the first file exceeds a first threshold;

[0103] Step 2: Push the first file blocks one by one from the upload queue for uploading, and calculate the checksum of each first file block;

[0104] Step 3, when the last first file block is pushed out of the upload queue for uploading, the check codes of all first file blocks are combined to obtain a complete check code of the first file; and

[0105] Step 4: Upload the complete verification code.

[0106] In an embodiment of the present application, if the size of a certain file exceeds a first threshold, the file needs to be fragmented, and files of different file types may correspond to different fragment sizes. For example, file types such as system files and executable files need to avoid data cutting as much as possible. Therefore, a higher fragmentation threshold can be set for this type of file, so that when this type of file is fragmented, the size of each file block is as large as possible. For files of other file types, a relatively small fragmentation size can be set to improve the efficiency of file upload. The file whose file size exceeds the above-mentioned first threshold is the first file that needs to be fragmented. The first file is fragmented to obtain multiple first file blocks. When uploading the first file, it is necessary to push the multiple first file blocks of the first file out of the upload queue one by one for uploading, and calculate the check code of each first file block while transmitting the first file block. Finally, the check codes of all the first file blocks are merged to obtain the complete check code of the first file.

[0107] The above-mentioned first threshold can be set according to actual needs. Different thresholds can be pre-set for different file types, and the threshold can also be dynamically adjusted according to the network bandwidth. For situations where different thresholds can be pre-set for different file types, such as for system files, executable files, etc., it is necessary to avoid data fragmentation as much as possible to ensure data consistency. Therefore, a higher threshold can be set for this type of file; and for more trivial files, a smaller threshold can be set. For situations where the threshold can be dynamically adjusted according to the network bandwidth, such as dynamically adjusting the threshold according to changes in the transmission rate, the threshold size of the current file can also be determined according to the upload status of the previous file.

[0108] In the embodiment of the present application, the proportion of the uploaded file blocks in all file blocks can be calculated to determine the upload progress, and the upload progress and the upload status of each file block can be displayed.

[0109] In the embodiment of the present application, if the uploading process is interrupted, by recording the breakpoint, the uploading can be resumed from the breakpoint when uploading again.

[0110] Optionally, pushing the file data into the upload queue for file uploading according to the directory structure of the directory tree further includes:

[0111] Step 1, determining the file type of the second file, and calling a first sharding strategy matching the file type to shard the second file to obtain a plurality of second file blocks;

[0112] Step 2, selecting a target channel from the channel pool to upload multiple second file blocks, wherein the target channel is a channel in the channel pool that has not been selected within a preset time;

[0113] Step 3, during the upload process of the plurality of second file blocks, adjusting the first sharding strategy according to the upload status of the uploaded second file blocks to obtain a second sharding strategy;

[0114] Step 4: Re-segment the remaining second file blocks that have not been uploaded using the second segmentation strategy, and reselect the target channel for uploading.

[0115] In an embodiment of the present application, the file type of the second file to be fragmented can be determined first, and then the initial fragment size of the second file can be determined according to the file type of the second file, so the second file is fragmented according to the initial fragment size to obtain multiple second file blocks, and finally a channel that has not been selected within a preset time is selected from the channel pool to upload these multiple second file blocks to avoid channel blocking. The above initial fragment size is the first fragmentation strategy. For example, system files, executable files, etc. should ensure their integrity as much as possible and the larger the fragments, the better. A relatively large initial fragment size can be set, and a relatively small initial fragment size can be set according to the browser performance for ordinary files. After a part of the multiple second file blocks are uploaded, the fragment size can be dynamically adjusted according to the upload status of the uploaded second file blocks, that is, the first fragmentation strategy is adjusted to obtain the second fragmentation strategy, and finally the adjusted fragment size is used to re-fragment and upload the remaining unuploaded second file blocks.

[0116] In an embodiment of the present application, after determining the initial segment size of the second file, a portion of the second file may be segmented according to the initial segment size. After uploading this portion, the segment size may be adjusted according to the upload status of this portion. Finally, the remaining second file may be segmented and uploaded according to the adjusted segment size.

[0117] In the embodiment of the present application, the initial fragment size can also be determined according to the file type and the network bandwidth. For example, when uploading a system file, it is determined based on prior knowledge that uploading the system file requires a 20 megabyte bandwidth, and at this time, the initial fragment size of the system file is preferably set to 500 MB. If the current bandwidth is only 5 megabytes, the initial fragment size of the system file needs to be set to 100 MB.

[0118] According to another aspect of the embodiment of the present application, Figure 3 As shown, a file uploading device is provided, comprising:

[0119] The first reading module 301 is used to read the file data to be uploaded based on the depth-first strategy, and monitor the memory state of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy;

[0120] The second reading module 303 is used to clear the context environment record access path stored in the call stack when it is monitored that the memory usage of the call stack reaches a warning value, and read the file data based on the breadth-first strategy to obtain a directory tree of the file data;

[0121] The upload module 305 is used to push the file data into the upload queue for file upload according to the directory structure of the directory tree.

[0122] It should be noted that the first reading module 301 in this embodiment can be used to execute step S202 in the embodiment of the present application, the second reading module 303 in this embodiment can be used to execute step S204 in the embodiment of the present application, and the upload module 305 in this embodiment can be used to execute step S206 in the embodiment of the present application.

[0123] It should be noted that the examples and application scenarios implemented by the above modules and corresponding steps are the same, but are not limited to the contents disclosed in the above embodiments. It should be noted that the above modules as part of the device can be run in Figure 1 In the hardware environment shown, it can be implemented by software or by hardware.

[0124] Optionally, the first reading module, before reading the file data to be uploaded based on the depth-first strategy, is further used to:

[0125] Sending preset condition parameters to the service end to instruct the service end to prepare the file data to be uploaded according to the preset condition parameters, wherein the preset condition parameters include a file level threshold and a file quantity threshold;

[0126] When the actual file level of the file data fed back by the business end is within the file level threshold range and the actual file number of the file data is within the file number threshold range, the file data to be uploaded is read based on the depth-first strategy.

[0127] Optionally, the first reading module is specifically used to:

[0128] The root folder is used as the reading starting point. Whenever the next level folder is read, a context environment is generated to record the access path to record the current layer position.

[0129] The context environment record access path is stored in the call stack, and the next level folder is entered to continue reading file data;

[0130] When there is no next-level folder in the current-level folder, the previous-level folder of the current-level folder is returned by recording the access path in the context environment in the call stack;

[0131] When the access path is recorded through the context environment in the call stack back to the root folder and all individual files in the root folder are read, a directory tree is generated with the root folder as the root node, subfolders at each level as child nodes, and individual files at each level as leaf nodes.

[0132] Optionally, the second reading module is specifically used to:

[0133] When the call stack memory usage reaches the warning value, the file level and number of files read based on the depth-first strategy are updated to new preset condition parameters, and a reading queue is created;

[0134] Push the root folder into the read queue as the reading starting point;

[0135] Push the folder at the head of the read queue to read a single file in the current folder, and push the folder below the current folder into the read queue;

[0136] When all individual files in the current folder have been read and all lower-level folders have been pushed into the read queue, the folder at the head of the queue is pushed out to continue reading the file data in each folder in the order in which they were queued;

[0137] When all folders are pushed out of the read queue, a directory tree is generated with the root folder as the root node, folders at each level as child nodes, and individual files at each level as leaf nodes.

[0138] Optionally, when reading file data, the first reading module and the second reading module are used to:

[0139] Generate a corresponding microtask for each folder or each single file, and push each microtask into the microtask queue in the order of generation;

[0140] When the main thread is in an idle state, the main thread is called to execute the microtask queue and the main thread's occupied time is recorded, wherein the main thread's occupied time is accumulated when it continuously executes microtasks;

[0141] When the occupied time is greater than the preset time threshold, corresponding macro tasks are created for the remaining microtasks in the microtask queue, so that the main thread executes a macro task each time after executing a microtask, wherein the occupied time is reset after the main thread executes the macro task.

[0142] Optionally, the upload module is specifically used to:

[0143] Determine a fragment size of the first file according to the file type of the first file in the file data, and fragment the first file according to the fragment size to obtain a plurality of first file blocks, wherein the size of the first file exceeds a first threshold;

[0144] Pushing out the first file blocks one by one from the upload queue for uploading, and calculating the checksum of each first file block;

[0145] When the last first file block is pushed out of the upload queue for uploading, merging the check codes of all first file blocks to obtain a complete check code of the first file; and

[0146] Upload the complete verification code.

[0147] Optionally, the upload module is further used to:

[0148] Determine a file type of the second file, and call a first sharding strategy that matches the file type to shard the second file to obtain a plurality of second file blocks;

[0149] Selecting a target channel from the channel pool to upload multiple second file blocks, wherein the target channel is a channel in the channel pool that has not been selected within a preset time;

[0150] During the upload of the plurality of second file blocks, adjusting the first sharding strategy according to the upload status of the uploaded second file blocks to obtain a second sharding strategy;

[0151] The second fragmentation strategy is used to re-segment the remaining second file blocks that have not been uploaded, and a target channel is reselected for uploading.

[0152] According to another aspect of the embodiments of the present application, the present application provides an electronic device, such as Figure 4 As shown, it includes a memory 401, a processor 403, a communication interface 405 and a communication bus 407. The memory 401 stores a computer program that can be run on the processor 403. The memory 401 and the processor 403 communicate through the communication interface 405 and the communication bus 407. When the processor 403 executes the computer program, the steps of the above method are implemented.

[0153] The memory and processor in the above electronic device communicate via a communication bus and a communication interface. The communication bus may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus may be divided into an address bus, a data bus, a control bus, etc.

[0154] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.

[0155] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0156] According to another aspect of the embodiments of the present application, a computer program product or a computer program is provided, the computer program product or the computer program includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device performs the steps of any of the above embodiments.

[0157] Optionally, in an embodiment of the present application, the computer-readable medium is configured to store program codes for the processor to perform the following steps:

[0158] Read the file data to be uploaded based on the depth-first strategy, and monitor the memory state of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy;

[0159] When it is detected that the memory usage of the call stack reaches a warning value, the context environment record access path stored in the call stack is cleared, and the file data is read based on a breadth-first strategy to obtain a directory tree of the file data; and,

[0160] According to the directory structure of the directory tree, the file data is pushed into the upload queue for file upload.

[0161] Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments, and this embodiment will not be described in detail here.

[0162] When the embodiments of the present application are specifically implemented, reference may be made to the above-mentioned embodiments, which have corresponding technical effects.

[0163] It is understood that the embodiments described herein may be implemented in hardware, software, firmware, middleware, microcode, or a combination thereof. For hardware implementation, the processing unit may be implemented in one or more application specific integrated circuits (ASIC), digital signal processors (DSP), digital signal processing devices (DSPD), programmable logic devices (PLD), field programmable gate arrays (FPGA), general purpose processors, controllers, microcontrollers, microprocessors, other electronic units for performing the functions described in the present application, or a combination thereof.

[0164] For software implementation, the technology described herein can be implemented by a unit that performs the functions described herein. The software code can be stored in a memory and executed by a processor. The memory can be implemented in the processor or outside the processor.

[0165] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0166] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0167] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0168] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0169] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0170] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium, including several instructions to make a computer device (which can be a personal computer, a server, or a network device, etc.) perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a ROM, a RAM, a magnetic disk or an optical disk. It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the term "include", "include" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements that are not explicitly listed, or also includes elements inherent to such a process, method, article or device. Without more constraints, an element defined by the phrase "comprising a..." does not exclude the existence of other identical elements in the process, method, article or apparatus comprising the element.

[0171] The above description is only a specific implementation of the present application, so that those skilled in the art can understand or implement the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest range consistent with the principles and novel features applied for herein.

Claims

1. A file uploading method, comprising: Read the file data to be uploaded based on a depth-first strategy, and monitor the memory state of the call stack during the reading process, wherein the call stack is used to store the context environment record access path recursively generated when reading the file data based on the depth-first strategy; When it is monitored that the memory occupancy rate of the call stack reaches a warning value, clearing the context environment record access path stored in the call stack, and reading the file data based on a breadth-first strategy to obtain a directory tree of the file data; and According to the directory structure of the directory tree, the file data is pushed into the upload queue for file uploading.

2. The method according to claim 1, characterized in that Before reading the file data to be uploaded based on the depth-first strategy, the method further includes: Sending preset condition parameters to the service end to instruct the service end to prepare the file data to be uploaded according to the preset condition parameters, wherein the preset condition parameters include a file level threshold and a file quantity threshold; and When the actual file level of the file data fed back by the service end is within the file level threshold range and the actual file number of the file data is within the file number threshold range, the file data to be uploaded is read based on a depth-first strategy.

3. The method according to claim 2, characterized in that The method of reading the file data to be uploaded based on the depth-first strategy includes: Taking the root folder as the reading starting point, whenever the next level folder is read, the context environment record access path is generated to record the current layer position; The context environment record access path is stored in the call stack, and the next level folder is entered to continue reading the file data; In the case that the next-level folder does not exist in the folder of the current level, returning to the previous-level folder of the folder of the current level through the context environment record access path in the call stack; and When the access path is recorded through the context environment in the call stack and returned to the root folder and all individual files in the root folder are read, the directory tree is generated with the root folder as the root node, subfolders at each level as child nodes, and individual files at each level as leaf nodes.

4. The method according to claim 2, characterized in that: The reading of the file data based on the breadth-first strategy includes: When the call stack memory occupancy rate reaches a warning value, the file level and the number of files read based on the depth-first strategy are updated to new preset condition parameters, and a reading queue is created; Pushing the root folder into the read queue as a reading starting point; Pushing the folder at the head of the read queue to read a single file in the current folder, and pushing the folder at the next level of the current folder into the read queue; When all the single files in the current folder have been read and all the lower-level folders have been pushed into the read queue, continue to push out the folder at the head of the queue to continue to read the file data in each folder in sequence according to the order of entry; and When all folders are pushed out of the read queue, the directory tree is generated with the root folder as the root node, the folders at each level as child nodes, and the single files at each level as leaf nodes.

5. The method according to claim 1, characterized in that The reading of the file data comprises: Generate a corresponding microtask for each folder or each single file, and push each microtask into the microtask queue in the order of generation; When the main thread is in an idle state, calling the main thread to execute the microtask queue, and recording the occupied time of the main thread, wherein the occupied time is accumulated when the main thread continuously executes the microtask; and When the occupied time is greater than a preset time threshold, corresponding macro tasks are created for the remaining microtasks in the microtask queue, so that the main thread executes the macro task once each time after executing one microtask, wherein the occupied time is reset after the main thread executes the macro task.

6. The method according to claim 1, characterized in that The step of pushing the file data into an upload queue for file uploading according to the directory structure of the directory tree includes: Determining a fragment size of the first file according to a file type of the first file in the file data, and fragmenting the first file according to the fragment size to obtain a plurality of first file blocks, wherein the size of the first file exceeds a first threshold; Pushing the first file blocks one by one from the upload queue for uploading, and calculating a checksum of each first file block; When the last first file block is pushed out of the upload queue for uploading, merging the check codes of all the first file blocks to obtain a complete check code of the first file; and Upload the complete verification code.

7. The method according to claim 1, characterized in that The step of pushing the file data into an upload queue for file uploading according to the directory structure of the directory tree further includes: Determine a file type of a second file, and call a first sharding strategy matching the file type to shard the second file to obtain a plurality of second file blocks; Selecting a target channel from a channel pool to upload the plurality of second file blocks, wherein the target channel is a channel in the channel pool that has not been selected within a preset time; During the uploading process of the plurality of second file blocks, adjusting the first sharding strategy according to the upload status of the uploaded second file blocks to obtain a second sharding strategy; The second fragmentation strategy is used to re-segment the remaining second file blocks that have not been uploaded, and the target channel is reselected for uploading.

8. A file uploading device, characterized in that: include: A first reading module is used to read the file data to be uploaded based on a depth-first strategy, and monitor the memory state of a call stack during the reading process, wherein the call stack is used to store a context environment record access path recursively generated when reading the file data based on the depth-first strategy; A second reading module is used to clear the context environment record access path stored in the call stack when it is monitored that the memory usage of the call stack reaches a warning value, and read the file data based on a breadth-first strategy to obtain a directory tree of the file data; and The upload module is used to push the file data into the upload queue for file upload according to the directory structure of the directory tree.

9. An electronic device, comprising a memory, a processor, a communication interface and a communication bus, wherein the memory stores a computer program that can be run on the processor, and the memory and the processor communicate through the communication bus and the communication interface, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.

10. A computer readable medium having a non-volatile program code executable by a processor, characterized in that: The program code enables the processor to execute the method according to any one of claims 1 to 7.