In recent years, with the popularity of mobile Internet, the development of mobile applications has attracted more and more attention. The emergence of front-end frameworks has greatly improved the development efficiency of mobile applications. As a front-end development framework, Uniapp aims to solve the pain points of multi-terminal development and allow developers to more conveniently develop applications with cross-platform capabilities. But at the same time, some problems have also arisen. This article will discuss the issue of Uniapp's real machine failure to upload pictures.
1. Problem description
Uniapp is based on the Vue framework, compiled using the HBuilder compiler, and allows development using different platforms such as Weex, applets, and APPs. In Uniapp development, we usually use the function of uploading pictures, but in actual applications we often encounter this problem: During the development process, we can successfully upload through the simulator after debugging, but when debugging on the real machine, we can upload pictures successfully. A failure occurs.
2. Problem Analysis
In local debugging, we often use the HBuilder compiler that comes with it emulator. In real machine debugging, we need to install the application into the mobile phone first and debug it through the HBuilder debugging tool. The processor, memory, operating system and other factors of different devices will affect the running effect of the application.
In Uniapp development, we usually use Ajax for data interaction, that is, data interaction between the front end and the back end through the XMLHttpRequest object. In Ajax, due to the browser's Same-Origin Policy and Webview restrictions, cross-domain problems will occur when the requested URL is different from the request source set in HBuilder. Therefore, in real machine debugging, we need to match the URL requested by Ajax with the request source configured in HBuilder, so that cross-domain requests can be made.
Another reason why Uniapp fails to upload images on real devices is that many mobile phones have restrictions on the size of uploaded images. The specific performance is as follows: usually small images can be uploaded, but when uploading larger images, the upload will fail.
3. Solution
1. Local debugging and real machine debugging
In order to solve the difference between local debugging and real machine debugging, we can perform real machine debugging in the simulator. Machine testing. In HBuilder, we can use the real-device synchronization debugging (Synchronize to Device) function to synchronize the application to the mobile phone for real-device debugging. In this way, the specific reason why the image upload failed can be found out during the debugging process.
2. Cross-domain issues
In order to solve cross-domain issues, we need to configure it on the server side. Specifically, we need to set Access-Control-Allow-Origin in the server's response header to support cross-origin requests.
3. Image size limit
In order to solve the problem of uploaded image size limit, we can use image compression technology. By using image compression tools to control the image size within a certain range, you can successfully upload the image.
4. Summary
There are two main reasons why Uniapp’s real machine fails to upload images: cross-domain issues and image size restrictions. By applying the solutions outlined in this article, we can better solve these problems. At the same time, in Uniapp development, we need to pay more attention to platform differences and continue to debug and optimize to truly achieve multi-platform development.
The above is the detailed content of What to do if uniapp fails to upload pictures from real machine. For more information, please follow other related articles on the PHP Chinese website!