jQuery File Upload Example

text/plain MIME Type and Python

and then check the MIME type it would say application/octet-stream . What’s the difference?

For the impatient

echo adds a new line to file which tells the file utility it is a text file.

For the curious

When I saw this question on StackOverflow, I was really stumped due to the following reasons:

  1. I didn’t know the file utility can be used to get the mime-type of the file. I thought MIME Type is only relevant in the context of web server and clients. After all, MIME stands for Multipurpose Internet Mail Extensions
  2. I thought operating systems usually use the file extension to decide the file type, by extension the mime type. Don’t the OSes warn when we touch the extension part of the files while renaming, all the time? So, how does file utility do this on a files without any extension?

Adding extensions

Lets try adding extensions:

$ echo "x" > some_file.txt $ file --mime-type some_file.txt some_file.txt: text/plain

Okay, that’s all good. Now to the Python side:

with open("some_file_2.txt", "w") as fp: fp.write("x")
$ file --mime-type some_file_2.txt some_file_2.txt: application/octet-stream

What? file doesn’t recognise file extensions?

The OS conspiracy theory

Maybe echo writes the mimetype as a metadata onto the disk because echo is a system utility and it knows to do that and in Python the user (me) doesn’t know how to? Clearly the operating system utilities are a cabal of some forbidden knowledge. And I am going to uncover that today, starting with the file utility which seems to have different answers to different programs.

Читайте также:  Php зачем нужен конструктор

How does ‘file’ determine MIME Type?

Answers to this question has some useful information:

  1. MIME Type is a fictional value. There is no inherent metadata field that stores MIME Types of files.
  2. Each operating system uses a different technique to decide file type. Windows uses file extension, Mac OS uses type creator & type codes and Unix uses magic numbers.
  3. The file command guesses file type by reading the content and looking for magic numbers and strings.

Time to reveal the magic

Let us peer into the souls of these files in their purest forms where there is no magic but only 1s and 0s. So, I printed the binary representation of the two files.

$ xxd -b my_file 00000000: 01111000 00001010 x. $ xxd -b my_file_2 00000000: 01111000 x

The file generated by echo has two bytes (notice the . after the x) whereas the file I created with Python only has one byte. What is that second byte?

>>> number = int('00001010', 2) >>> chr(number) '\n'

And it turns out like every movie on magic, there is no such thing as magic. Just clever people putting new lines to tell file it is a text file.

Creating a trick

Now that the trick is revealed, lets create our own magic trick

$ echo " " > xml_file $ file --mime-type xml_file xml_file: text/plain $ echo " " > xml_file $ file --mime-type xml_file xml_file: text/xml

Источник

Managing application/octet-stream in Flask

A sample handler has been provided for handling diverse data types, including form data. Nevertheless, according to @Andrey, there are more elegant approaches for transmitting numerous data objects rather than creating a custom ad-hoc format.

Flask: How to handle application/octet-stream

My goal is to create a form that enables the uploading of multiple files. To make this possible, I am utilizing jQuery File Uploader. As for my server-side code.

@app.route("/new/photogallery",methods=["POST"]) def newPhotoGallery(): print request.files 
  1. Submit form normally: When i submit my form normally,it prints: ImmutableMultiDict([(‘post_photo_gallery’, FileStorage: u» (‘application/octet-stream’))])
  2. Submit form using AJAX: When i submit my form using AJAX,it prints: ImmutableMultiDict([])

I have a few questions related to handling AJAX requests. Firstly, I’m curious about the difference between AJAX requests and normal requests. Secondly, I’m wondering how to handle a specific request ( application/octet-stream ) in Flask/Python . Lastly, I’d like to know if using application/octet-stream is a recommended approach.

Just wanted to mention that my knowledge on application/octet-stream is limited. Thank you for understanding.

Previously, I have utilized multipart/form-data type forms with Flask to successfully upload images, but unfortunately, I encountered difficulties in implementing a request with application/octet-stream type posts.

Utilizing werkzeug’s FileStorage objects, I have successfully expanded my previous work to enable the simultaneous uploading of multiple files.

The crucial step is to create a route for posting that searches for a form request element. This enables you to make a POST request to the route using either a traditional form or an AJAX call.

Displayed here is a basic illustration that employs a form.

>

Uploaded: >

from flask import Flask, request, render_template from werkzeug import secure_filename, FileStorage import os # Flask functions app = Flask(__name__) app.config.from_object(__name__) DEBUG = True # add this so that flask doesn't swallow error messages app.config['PROPAGATE_EXCEPTIONS'] = True @app.route('/', methods=['GET', 'POST']) def uploader(): if request.method =='POST' and request.files.getlist('files'): up_file_list = [] # Iterate the through a list of files from the form input field for a_file in request.files.getlist('files'): if a_file.filename: # Validate that what we have been supplied with is infact a file if not isinstance(a_file, FileStorage): raise TypeError("storage must be a werkzeug.FileStorage") # Sanitise the filename a_file_name = secure_filename(a_file.filename) # Build target a_file_target = os.path.join('/tmp/', a_file_name) # Save file a_file.save(a_file_target) up_file_list.append(a_file_name) # Return template if up_file_list: return render_template('uploader.html', err=None, files=up_file_list) else: return render_template('uploader.html', err='No Files Uploaded', files=None) else: return render_template('uploader.html', err=None, files=None) # application execution if __name__ == '__main__': app.run() 

Irrespective of the method used for data encoding, obtaining the original data is possible using request.data . If you are dealing with application/octet-stream , then you may simply save the data to a binary file by executing request.data .

An instance of a handler that can manage different types of data.

from flask import json @app.route('/messages', methods = ['POST']) def api_message(): if request.headers['Content-Type'] == 'text/plain': return "Text Message: " + request.data elif request.headers['Content-Type'] == 'application/json': return "JSON Message: " + json.dumps(request.json) elif request.headers['Content-Type'] == 'application/octet-stream': with open('/tmp/binary', 'wb') as f: f.write(request.data) f.close() return "Binary message written!" else: return "415 Unsupported Media Type ;)" 

The standard procedure for managing form data has already been recorded in this documentation.

Writing to a «application/octet-stream» file on linux, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

POST call with application/octet-stream

I’m attempting to initiate a POST request to an API using the code provided.

var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream")); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key); var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["returnFaceId"] = "true"; queryString["returnFaceLandmarks"] = "false"; queryString["returnFaceAttributes"] = "age,gender"; var filebytes = File.ReadAllBytes(@"C:\Users\user1\Desktop\IMG.jpg"); var uri = "https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect?" + queryString; HttpResponseMessage response; using (var content = new ByteArrayContent(filebytes))

The error occurs in the code labeled result .

The program functions properly when utilizing application/json and providing an HTTP link.

Can you attempt to configure the ContentType to match the provided Microsoft sample code?

using (var content = new ByteArrayContent(byteData)) < content.Headers.ContentType = new MediaTypeHeaderValue("< your content type, i.e. application/json >"); response = await client.PostAsync(uri, content); > 

Attempt to specify the content type of the request as «application/octet-stream».

How to get the response from an ajax request with, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Is it ok to return application/octet-stream from a REST interface?

Is it considered a violation of REST principles if I respond with application/octet-stream? The endpoint I’m working with receives five URLs of images.

It will retrieve these files and provide them in the format of application/octet-stream.

To clarify, the REST interface is being invoked by a mobile app client. It is important to note that each network connection made will cause a decrease in battery life by a few milliamps. Despite the potential impact on battery life, REST is being used due to it being a company standard. If it were not a standard, an alternative binary protocol would be utilized.

The client may struggle to utilize the binary data beyond storing or forwarding it to another process. This is acceptable if storing or forwarding is the primary purpose of the data.

Consider exploring multipart content types as a solution. In my opinion, utilizing a multipart message with multiple image/gif components would be a preferable option.

It appears that this is more of an RPC call where a list of URLs is provided and an archive is expected in return.

The process in question cannot be classified as RESTful because REST does not rely on an RPC-based framework.

To effectively use the archives, consider them as valuable assets that can be utilized to generate and provide information.

POST /archives Content-Type: application/json < "image1": "http://ww.o.com/1.gif", "image2": "http://www.foo.be/2.gif" >

As a result, you would get

HTTP/1.1 201 Created Location: http://example.com/archives/1234 Content-Type: application/json 

After that, it is possible to send a request to http://example.com.

GET /archives/1234 Accept: multipart/mixed 

You can obtain the complete archive with just one request, but the result will be formatted as multipart. Alternatively, you may choose to use multipart/x-zip, which is a zip file.

GET /archives/1234 Accept: application/json 

You will receive the original JSON that was sent, which allows for potential editing and updating of the archive. This option may be preferred over sending the binary images.

To modify it, all you need to do is send a POST request with the updated information.

PUT /archives/1234 Content-Type: application/json < "image1": "http://ww.o.com/1.gif", "image2": "http://www.foo.be/2.gif", "image3": "http://www.foo2.foo/4.gif" >

The name of the resource is /archives/1234.

In this scenario, there are two versions of it — the JSON representation and the physical one, which is referred to as binary archive. To differentiate between the two, your service relies on the content type specified in the Accept header. This header serves as a request from the client, indicating its preferences.

Once you have finished using the archive, you can proceed to REMOVE it.

Alternatively, the resource can be set to expire at a future time by the server.

What if we make five distinct REST requests instead?

It appears to have a neater layout and is divided in a more organized manner. Moreover, depending on the browser utilized, it can simultaneously run 2 or more downloads.

The rules governing REST are referred to as principles rather than laws. In my opinion, it is not a violation to deviate from them. REST is concerned with resources that can be identified by a URL and can be accessed in various formats if necessary. The format specification is not included. This article provides a straightforward explanation of REST.

According to @Andrey, there are better approaches to sending multiple data objects than creating a custom format. Two alternatives are using the Multipart mimeType/format or packing the objects into an archive file such as tar or zip.

The issue with utilizing «application/octet-stream» is its deficiency of information regarding the actual data format. It’s the client’s responsibility to be aware of the format and interpret it accordingly. Creating a unique format raises concerns about interoperability and the need to design, implement, and maintain libraries to support it, which may have to be done multiple times.

Azure — How to convert content type application octet, json (triggerBody ()) 3. Change the content-type accordingly 4. The problem might be in API call while extracting the data For converting the desired output into .csv one of the workarounds is you can always store them into blob using the .csv extension and then it will automatically convert the data into CSV …

Источник

Оцените статью