I have a webpage where the user can upload a PDF file and send it using AJAX to my Flask application. Here is the ajax code:
var formData = new FormData();
formData.append('attachment', document.getElementById("attachment").files[0]);
$.ajax({
type: 'POST',
url: 'process_award_storage',
contentType: false,
processData: false,
data: formData
})
I do not think that this has any problems because I can print out the content and title of file in the python code. Then my flask model is defined as such, using LargeBinary for the PDF attachment:
class AwardStore(db.Model):
__tablename__ = 'awards_store'
id = db.Column(db.Integer, primary_key=True)
...
file = db.Column(db.LargeBinary, nullable=True) #I am sure this is the right file type for PDF saving
Lastly, here is how the AJAX file is saved to the database:
award = AwardStore(name=name, file=request.files['attachment'].read())
db.session.add(award)
db.session.commit()
I can see using my MySQL Workbench that it is saved. However, when I try to download the BLOB from there to the Desktop and open it, it says "Failed to load PDF document". The same happens when I use flask's send_file. It seems like I did everything as I saw online, but something is wrong.
Maybe these warnings are related?:
C:\Users\msolonko\Desktop\NICKFI~1\CACHTM~1\APP_DE~1\virt\lib\site-packages\pymysql\cursors.py:170: Warning: (1300, "Invalid utf8 character string: 'C4E5F2'")
result = self._query(query)
C:\Users\msolonko\Desktop\NICKFI~1\CACHTM~1\APP_DE~1\virt\lib\site-packages\pymysql\cursors.py:170: Warning: (1265, "Data truncated for column 'file' at row 1")
result = self._query(query)
I tried googling them and did not find anything. I appreciate any assistance.
EDIT:
I noticed that small files are typically uploaded and displayed properly. The issue is with files with sizes > ~50kB. The database I am using the AWS RDS, Is there a setting I can change somewhere to enable greater sizes?
The LargeBinary accepts a length field also.
which converts to mysql's BLOB whose default length is 65535 bytes (64kb).
Try increasing the length and set it to
a MEDIUMBLOB for 16777215 bytes (16 MB)
a LONGBLOB for 4294967295 bytes (4 GB).
Hope this will help
Related
Can someone tell me what I am doing wrong with trying to upload an image to blob storage? Below is my code.
print(type(img['image'])) #Output is <class 'bytes'>
connection_string = get_blob_connection_string()
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container="images", blob=img['id'])
exists = blob_client.exists()
if (exists == False):
result = blob_client.upload_blob(img['image'], blob_type="blockblob")
print(result)
When inserting the blob, it throws the error
quote_from_bytes() expected bytes
This error makes no sense, I gave it bytes. What am I missing?
After reproducing from my end, I have received the same issue. You are receiving this error because of incompatible type of the file (i.e., file format).
After changing the below line to the correct format I could able to achieve your requirement.
blob_client = blob_service_client.get_blob_client(container="images", blob=img['id'])
Below is the correct format
blob_client=blob_service_client.get_blob_client(container='container', blob='<LOCAL FILE PATH>');
Below is the complete code that worked for me
from azure.storage.blob import BlobServiceClient
from PIL import Image
connection_string = "<CONNECTION STRING>"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client=blob_service_client.get_blob_client(container='container', blob='<LOCALPATH>');
with open(file='<PATH IN YOUR STORAGE ACCOUNT WITH FILE NAME>', mode="rb") as data:
blob_client.upload_blob(data)
RESULTS:
In IBM API Connect I am trying to use 'crypto' module in IBM API Connect gatewayscript. When I tested whether the crypto module is supported in gatewascript or not, I got the response as below
Code in Gatewayscript:
var crypto = require('crypto');
session.output.write(crypto);
Output:
*{
"getHashes": {},
"getCiphers": {},
"createHash": {},
"createHmac": {},
"createSign": {},
"createVerify": {},
"createCipheriv": {},
"createDecipheriv": {},
"randomBytes": {}
}*
But when I tried to make use of it, I got 500 Internal Server Error:
Code:
var crypto = require('crypto');
var key = "Alice";
var hmac = crypto.createHmac('hmac-sha256', key);
var input = "This is plaintext to hash";
var result = hmac.update(input).digest('base64');
session.output.write(result);
output:
{
"httpCode": "500",
"httpMessage": "Internal Server Error",
"moreInformation": "Internal Error"
}
Not sure where the things are going wrong. I am copy pasting exact example from IBM website. Here is the reference to crypto:https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.7.0/com.ibm.dp.doc/crypto_js.html#crypto.createHmac
By using var key = "Alice"; you tell the datapower to use the sharedkey stored with alias 'Alice'.
If you want to use the 'Alice' string then you need to use a buffer like var key = new Buffer("Alice");
Nevertheless it won't work as HMAC expects a 160 bits key for hmac-sha1.
You can generate it like that
$ dd if=/dev/random count=20 bs=1 | xxd -ps
a73e3406e7dcc5fc168d9ae9954ec6e0d85e4444
20 as 20 Bytes (20x8 bits=160 bits)
If you want to store it in a shared object you can follow what's describe here : http://rcbj.net/blog01/2012/03/17/generating-and-uploading-a-shared-key-symmetric-key-to-datapower-appliances/
Put the hex string generated by this command into a file called secret.key.
Upload the key to the cert:/// directory on the appliance.
Navigate to Objects->Crypto Configuration->Crypto Shared Secret Key.
Click Add.
Enter a name for the shared key.
From the drop down, chose the secret.key file that was uploaded a moment ago.
Click Apply.
If no errors are displayed, the key was successfully read.
Click Save.
When calling the RingCentral Create Custom Greeting API:
POST /restapi/v1.0/account/{accountId}/extension/{extensionId}/greeting
I sometimes get the following error with larger files MP3 and WAV media files. Is there an official size limit?
HTTP/1.1 413 Request Entity Too Large
{
"errorCode": "AGW-413",
"message": "Request entity too large",
"errors": [
]
}
There's no limit specified in the API Reference or blog article:
API Reference:
https://developers.ringcentral.com/api-docs/latest/index.html#!#RefCreateUserCustomGreeting
I'm using the ringcentral_sdk gem with the following code:
req = RingCentralSdk::REST::Request::Multipart.new(
method: 'post',
url: 'account/~/extension/~/greeting'
).
add_json({type: 'Voicemail', answeringRule: {id: '11111111'}}).
add_file(file)
res = client.send_request req
puts res.status
puts MultiJson.encode(res.body, pretty: true)
More is on this blog article:
https://medium.com/ringcentral-developers/updating-ringcentral-user-extension-greetings-using-the-rest-api-and-ruby-db325022c6ee
I was informed there is currently a 1MB file size limit on this API.
I tested this with 0.4MB and 2.5MB WAV files here and confirmed that the smaller file worked and the larger file resulted in this error.
https://www.mediacollege.com/audio/tone/download/
Other useful test files seem to be available here:
https://www.audiocheck.net/testtones_highdefinitionaudio.php
I wrote a Python sample: https://github.com/tylerlong/ringcentral-python/blob/master/test/test_multipart_mixed.py
I also confirm that if the audio file is too large the operation will fail and you will get message Request entity too large.
I'm trying to access url and then parse it's contents based on tags.
My code:
page = requests.get('https://support.apple.com/downloads/')
self.tree = html.fromstring(page.content)
names = self.tree.xpath("//span[#class='truncate_name']//text()")
Problem: variable page is containing data that of url 'https://support.apple.com/'
I'm new to python 2.7. The whole encoding issues in file. I'm using unicode-escape as my default encoding. Encoding on resource at https://support.apple.com/downloads/ is utf-8 whereas encoding of resource at https://support.apple.com/ is variable. Is this has something to do with the problem? Please suggest solution for this.
It has nothing to do with encoding , what you are looking for is dynamically created so not in the source you get back. A series of ajax calls populates the data. To get the product names etc.. from the carousel where you see the span.truncate_name in your browser:
params = {"page": "products",
"locale": "en_US",
"doctype": "DOWNLOADS",
}
js = requests.get("https://km.support.apple.com/kb/index", params=params).content
Normally we could call .json() on the response object but in this case we need to use "unicode_escape" then call loads:
from json import loads, dumps
js2 = loads(js.decode("unicode_escape"))
print(js2)
Which gives you a huge dict of data like:
{u'products': [{u'name': u'Servers and Enterprise', u'urlpath': u'serversandenterprise', u'order': u'', u'products': .............
You can see the request in chrome tools:
We leave off callback:ACDownloadSearch.customCallBack as we want to get back valid json.
I'm sending a string using jQuery AJAX POST and JSON:
$.ajax({
type: "POST",
dataType: "JSON",
url: "someUrl.asp?param1=someParam1",
contentType: "charset=utf-8",
data: JSON.stringify({
some_code: $( "#some_code" ).length > 0 ? $("#some_code").val() : ''
})
})
Serverside is VBScript/ASP.
some_code is a textbox with following text: someValue čšžćđ that needs to be saved just like that.
When scanning network traffic (IE9) I see this:
some_code=someValue Äšžćđ
When looking in the database (Oracle 12c) I see this:
someValue ?????
Html page encoding is Windows-1250.
NLS_LANG and NLS_CHARACTERSET are Slovenian.
I've tried out advice from at least a dozen different links, but to no avail, so I'm turning to you guys and girls. Thank you!
Right from the comments think I understand the issue;
The JSON has to be sent as contentType: "charset=utf-8" so the page someUrl.asp will also need to process in UTF-8 to do this follow the below steps.
Based on your comment have made some changes to the below code.
First re-save the someUrl.asp file using UTF-8 encoding not ASCII.
Set the first line in someUrl.asp to;
<%#Language="VBScript" CodePage = 65001 %>
Then add the following lines;
<%
Response.Charset = "Windows-1250"
Response.CodePage = 1250
%>
Note: When making changes always remember to save the file with UTF-8 encoding.