Re: [flask] File reading problem
- From:
- shantala
- Date:
- 2011-09-10 @ 06:50
Hi,
Anyone could crack it?
Snippets of code below:
In combine.html
<br>Attachment File 3:<input type="file" name="file3"><br></td>
.
.
.
In views.py
@app.route('/tomail1', methods=['GET','POST'])
def showmassmail_page():
if request.method == 'POST':
file3 = request.files['file3']
.
.
.
m = create_msg(userid, ,, file1, file2, file3 )
Then in
def create_msg(userid, ,, file1, file2, file3):
if file3:
ofile3 = file3
fnm33 = file3.filename
fnm3 = os.path.basename(fnm33)
chunk3 = ofile3.read(1024000)
# excluded all the other parameters from the actual code
mess1 = sendMail(fromId, , ,
subjectLine, message, chunk,
fnm, chunk2, fnm2, chunk3,
fnm3)
In
def sendMail(fromLine, , , , fchunk, fnm,
fchunk2, fnm2, fchunk3, fnm3):
if fchunk:
part = MIMEBase('application', "octet-stream")
part.set_payload(fchunk)
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% fnm)
msg.attach(part)
if fchunk2:
part2 = MIMEBase('application', "octet-stream")
part2.set_payload(fchunk2)
Encoders.encode_base64(part2)
part2.add_header('Content-Disposition', 'attachment; filename="%s"'
% fnm2)
msg.attach(part2)
if fchunk3:
part3 = MIMEBase('application', "octet-stream")
part3.set_payload(fchunk3)
Encoders.encode_base64(part3)
print part3
part3.add_header('Content-Disposition', 'attachment; filename="%s"'
% fnm3)
msg.attach(part3)
return msg.as_string()
Then compose the mail and send using SMTP. I am sending the file as
email attachment.
I want to restrict the file attachment size to 1MB using flask. Can
anyone help me with detailed code.
)
Here I want to limit the size of the uploaded file upto 1 MB.
Thanks in advance.
On 09/06/2011 09:34 AM, Shantala wrote:
> Hi, As stated earlier, I want to send this file as attachment, I am
> reading its content into a buffer.
> This works in the cgi-script as I have mentioned earlier, but not in the
> flask implementation.
>
> On Monday 05 September 2011 04:58 PM, Armin Ronacher wrote:
>> Hi,
>>
>> On 2011-09-05 12:04 PM, shantala wrote:
>>
>>> It throws up internal server error if the file size is more than 498 Kb.
>>> For file size less than that , there is no problem. However, I am able
>>> to upload a file of any size on the server. I just want to read the file
>>> into a buffer, rather than upload onto the server.
>>>
>> Sounds like you are hitting a memory limit from your server. Why do you
>> need everything in memory at once?
>>
>>
>> Regards,
>> Armin
>>
--
rgds...Shantala