Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3601504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:37:48+00:00 2026-05-18T20:37:48+00:00

I have a problem with sending request as attachments in django. My application writes

  • 0

I have a problem with sending request as attachments in django. My application writes some data to files and zips it. However, when i return an attachment response, the browser downloads it but the zip file is corrupted. (The original zip contains my files and doesn’t give any errors.)

My code is here:

        file_path = "/root/Programs/media/statics/schedules/"
        zip_file_name = file_path + "test.zip"
        zip_file = zipfile.ZipFile(zip_file_name, "w")
        for i in range(len(planner_list)):
                file_name = file_path + str(planner_list[i][0].start_date)
                render_to_file('deneme.html',file_name ,{'schedule':schedule})
                zip_file.write(file_name, os.path.basename(file_name),zipfile.ZIP_DEFLATED)
                os.remove(file_name)
        zip_file.close()

        response = HttpResponse(file_path , content_type='application/zip')
        response['Content-Disposition'] = 'attachment; filename=test.zip'
        return response
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:37:48+00:00Added an answer on May 18, 2026 at 8:37 pm

    HttpResponse takes a string or an iterator returning strings (like an open file-like object). You are passing it the file path, so the response contains a file whose content is just the file path instead of the valid zip content you wrote to the file. You can just use open as the HTTPResponse object will close the file for you:

    response = HttpResponse(open(file_path, 'rb'), content_type='application/zip')
    

    Quoting the docs:

    Finally, you can pass HttpResponse an iterator rather than strings. HttpResponse will consume the iterator immediately, store its content as a string, and discard it. Objects with a close() method such as files and generators are immediately closed.

    If you need the response to be streamed from the iterator to the client, you must use the StreamingHttpResponse class instead.

    Writing to disk on every request may hurt your server I/O – looking at your code, everything seems small enough to fit memory. You can use (C)StringIO (BytesIO in Python3) instead of real files:

    from io import BytesIO
    
    mem_file = BytesIO()
    with zipfile.ZipFile(mem_file, "w") as zip_file:
        for i, planner in enumerate(planner_list):
            file_name = str(planner[0].start_date)
            content = render_to_string('deneme.html', {'schedule':schedule})
        zip_file.writestr(file_name, content)
    
    f.seek(0)  # rewind file pointer just in case
    response = HttpResponse(f, content_type='application/zip')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with sending request as attachments in django. My application writes
I have an ajax request sending some data to a php file: Ext.Ajax.request({ url:
I have problem when I try insert some data to Informix TEXT column via
I have a problem with sending some custom headers to with a jQuery ajax
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have a problem with a little .Net web application which uses the Amazon
i have problem in sending zip file in network.. all other formats i am
I have a problem returning data back to the function I want it returned
Here's an interesting problem: I have some jQuery that looks like this: $(document).ready(function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.