Possible to upload file and parse the json result?

2019-05-31 03:11发布

问题:

I have a form that is used to upload the file and a CGI program in the server side to process the uploaded file. The file will be renamed to a different file name that I want to pass back to the client, I want to use JSON to return the result, but the browser always prompts me to save the returnd document to a file...

Conclusion: I want to upload a file and from the HTTP response I want to parse it to get the new filename generated by server side.

        <form action="/a.bc?cmd=upload&user_name=binc2&size=100&type=normal" method="post" enctype="multipart/form-data">
                <input type="file" name="image">
                <input type="submit" value="Submit" />
                <input type="text" value="ade75596e1d9a04d4ae0c1e81aa8fcf0" name="key" />
        </form>

The post will get a JSON response.

回答1:

It sounds like youre not actually making an ajax request... youre being prompted to download because the header is (and should be) application/json. If youre not executing an ajax request or consumign the response with some other script then there is no reason to use json, jsut render whatever you want rendered.

Also you cant upload files directly with ajax. if you need to do this, do a search for "ajax upload" for various techniques.