Please tell me how to upload files in the form of put. I wrote a demo, but the one received in the background is always empty.

is there a problem with my parameter passed in put form? Can"t you transfer files in the form of put? I hope the Great God can answer one or two for me.

clipboard.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input id="file" name="img" type="file"/>
</body>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
    $("-sharpfile").on("change",function(){
        var formData = new FormData();
        formData.append("img", $("-sharpfile")[0].files[0]);
        console.log(formData,"form", $("-sharpfile")[0].files[0])
        $.ajax({
            url: "xxx",
            type: "put",
            data: formData,
            processData: false,
            contentType: false,
            xhrFields:{withCredentials: true},
            success: function(response){
                console.log(response)
                    // 
            }
        });
    })
</script>
</html>
Jun.08,2021

first of all, let's analyze the difference between POST and PUT

.

HTTP/1.1 defines a total of 8 request methods: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, of which the following four are relatively commonly used:

GET      
PUT      
POST     
DELETE   

RESTful api makes full use of the HTTP protocol, each resource corresponds to a specific URI, and then uses different HTTP operations to add, delete, modify and query, for example:

POST       /uri       
DELETE     /uri/xxx   
PUT        /uri/xxx   
GET        /uri/xxx   

you can see that the corresponding operations of GET and DELETE are very clear, but both POST and PUT can create resources, so when to use POST and when to use PUT

this requires understanding another important property of HTTP protocol: idempotent

.

what is idempotent

to understand the difference between PUT and POST, we also need to know an important property of HTTP protocol, idempotent (Idempotence):

.

Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request.

What does

mean? Idempotency in the HTTP protocol means that no matter how many times a resource is requested, it has the same side effect on it

.

GET operation is safe, that is, no matter how many times the operation is performed, the state of the resource will not change, so GET is also idempotent.
PUT and DELETE are idempotent. For example, no matter how many times I modify a resource with PUT or DELETE, the result after each operation is no different.
POST operation is neither safe nor idempotent. If there is a common problem of repeated loading of POST, how many POST operations have we performed? In the end, how many resources are created, which is why browsers such as Chrom have pop-up prompts when refreshing POST requests

so, if you use PUT to upload, there is no problem, and you can also use POST to upload.
however, when you upload using PUT, the backend needs to process data , get the corresponding header header data, name, size, etc., and then save and write to the file.

front-end code, there is no problem with data processing, and the back-end processing is as follows (take PHP as an example)

public function getPut(string! name = null, var filters = null, var defaultValue = null, boolean notAllowEmpty = false, boolean noRecursive = false) -> var
{
    var put;

    let put = this->_putCache;

    if typeof put != "array" {
        let put = [];
        parse_str(this->getRawBody(), put);

        let this->_putCache = put;
    }

    return this->getHelper(put, name, filters, defaultValue, notAllowEmpty, noRecursive);
}


public function getRawBody() -> string
{
    var rawBody, contents;

    let rawBody = this->_rawBody;
    if empty rawBody {

        let contents = file_get_contents("php://input");

        /**
         * We need store the read raw body because it can't be read again
         */
        let this->_rawBody = contents;
        return contents;
    }
    return rawBody;
}

this is how put is handled contents = file_get_contents ("php://input");

Menu