UnicodeDecodeError Error charmap' codec can't decode byte 0x81 in position 250:UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 20: ordinal not in range(128)UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 2: ordinal not in range(128)UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1UnicodeDecodeError: 'utf8' codec can't decode byte 0x9cUnicodeDecodeError: 'utf-8' codec can't decode byteHow to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”Cannot display HTML stringUnicodeDecodeError: cp932 codec can't decode byte 0x81 in position 81UnicodeDecodeError: 'charmap' codec can't decodeUnicodeDecodeError: 'charmap' codec can't decode byte 0x81

What word can be used to describe a bug in a movie?

Shabbat clothing on shabbat chazon

Why does this Pokémon I just hatched need to be healed?

Improving software when the author can see no need for improvement

What happen if I gain the control of aura that enchants an opponent's creature? Would the aura stay attached?

Replace data between quotes in a file

How do I explain to a team that the project they will work on for six months will certainly be cancelled?

Do other countries guarantee freedoms that the United States does not have?

Pretty heat maps

In the movie Harry Potter and the Order or the Phoenix, why didn't Mr. Filch succeed to open the Room of Requirement if it's what he needed?

Evaluate the following limit .

Does two puncture wounds mean venomous snake?

How do we avoid CI-driven development...?

How many hit points does the Battle Smith Artificer's Iron Defender have?

How to mark beverage cans in a cooler for a blind person?

Atari ST DRAM timing puzzle

Can an SPI slave start a transmission in full-duplex mode?

During the Space Shuttle Columbia Disaster of 2003, Why Did The Flight Director Say, "Lock the doors."?

Ex-contractor published company source code and secrets online

Improve survivability of bicycle container

How quickly could a country build a tall concrete wall around a city?

How to say "fit" in Latin?

sed delete all the words before a match

Should I self-publish my novella on Amazon or try my luck getting publishers?



UnicodeDecodeError Error charmap' codec can't decode byte 0x81 in position 250:


UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 20: ordinal not in range(128)UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 2: ordinal not in range(128)UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1UnicodeDecodeError: 'utf8' codec can't decode byte 0x9cUnicodeDecodeError: 'utf-8' codec can't decode byteHow to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”Cannot display HTML stringUnicodeDecodeError: cp932 codec can't decode byte 0x81 in position 81UnicodeDecodeError: 'charmap' codec can't decodeUnicodeDecodeError: 'charmap' codec can't decode byte 0x81






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0


















class ProductDownloadlView(MultiSlugMixin,DetailView):
model=Product
def get(self,request,*args,**kwargs):
obj=self.get_object()
filepath = os.path.join(settings.PROTECTED_ROOT,obj.media.path)
guessed_type = guess_type(filepath)[0]
wrapper=FileWrapper(open(filepath))
mimetype = 'application/force-download'
if guessed_type:
mimetype=guessed_type
response = HttpResponse(wrapper,content_type=mimetype)
if request.GET.get("preview"):
response["Content-Disposition"]="attachment; filename=%s" %(obj.media.name)
response["X-SendFile"]=str(obj.media.name)
return response





am using python 3.7 and django 1.8.6 trying to download files like .txt works fine and .md files but i have tried an image file and it gives the error charmap' codec can't decode byte 0x81 in position 250: character maps to
from the template list products






% extends "base.html" %
% block Content %
<h1>Products</h1>
<p>object.title </p>
<p>object.description </p>
<p>object.price</p>
% if object.media %
<p> object.media </p>
<p><a href=" object.get_download ">Download</a></p>
<p><a href=" object.get_download ?preview=True">Preview</a></p>
% endif %
% endblock Content %












share|improve this question






























    0


















    class ProductDownloadlView(MultiSlugMixin,DetailView):
    model=Product
    def get(self,request,*args,**kwargs):
    obj=self.get_object()
    filepath = os.path.join(settings.PROTECTED_ROOT,obj.media.path)
    guessed_type = guess_type(filepath)[0]
    wrapper=FileWrapper(open(filepath))
    mimetype = 'application/force-download'
    if guessed_type:
    mimetype=guessed_type
    response = HttpResponse(wrapper,content_type=mimetype)
    if request.GET.get("preview"):
    response["Content-Disposition"]="attachment; filename=%s" %(obj.media.name)
    response["X-SendFile"]=str(obj.media.name)
    return response





    am using python 3.7 and django 1.8.6 trying to download files like .txt works fine and .md files but i have tried an image file and it gives the error charmap' codec can't decode byte 0x81 in position 250: character maps to
    from the template list products






    % extends "base.html" %
    % block Content %
    <h1>Products</h1>
    <p>object.title </p>
    <p>object.description </p>
    <p>object.price</p>
    % if object.media %
    <p> object.media </p>
    <p><a href=" object.get_download ">Download</a></p>
    <p><a href=" object.get_download ?preview=True">Preview</a></p>
    % endif %
    % endblock Content %












    share|improve this question


























      0












      0








      0











      class ProductDownloadlView(MultiSlugMixin,DetailView):
      model=Product
      def get(self,request,*args,**kwargs):
      obj=self.get_object()
      filepath = os.path.join(settings.PROTECTED_ROOT,obj.media.path)
      guessed_type = guess_type(filepath)[0]
      wrapper=FileWrapper(open(filepath))
      mimetype = 'application/force-download'
      if guessed_type:
      mimetype=guessed_type
      response = HttpResponse(wrapper,content_type=mimetype)
      if request.GET.get("preview"):
      response["Content-Disposition"]="attachment; filename=%s" %(obj.media.name)
      response["X-SendFile"]=str(obj.media.name)
      return response





      am using python 3.7 and django 1.8.6 trying to download files like .txt works fine and .md files but i have tried an image file and it gives the error charmap' codec can't decode byte 0x81 in position 250: character maps to
      from the template list products






      % extends "base.html" %
      % block Content %
      <h1>Products</h1>
      <p>object.title </p>
      <p>object.description </p>
      <p>object.price</p>
      % if object.media %
      <p> object.media </p>
      <p><a href=" object.get_download ">Download</a></p>
      <p><a href=" object.get_download ?preview=True">Preview</a></p>
      % endif %
      % endblock Content %












      share|improve this question

















      class ProductDownloadlView(MultiSlugMixin,DetailView):
      model=Product
      def get(self,request,*args,**kwargs):
      obj=self.get_object()
      filepath = os.path.join(settings.PROTECTED_ROOT,obj.media.path)
      guessed_type = guess_type(filepath)[0]
      wrapper=FileWrapper(open(filepath))
      mimetype = 'application/force-download'
      if guessed_type:
      mimetype=guessed_type
      response = HttpResponse(wrapper,content_type=mimetype)
      if request.GET.get("preview"):
      response["Content-Disposition"]="attachment; filename=%s" %(obj.media.name)
      response["X-SendFile"]=str(obj.media.name)
      return response





      am using python 3.7 and django 1.8.6 trying to download files like .txt works fine and .md files but i have tried an image file and it gives the error charmap' codec can't decode byte 0x81 in position 250: character maps to
      from the template list products






      % extends "base.html" %
      % block Content %
      <h1>Products</h1>
      <p>object.title </p>
      <p>object.description </p>
      <p>object.price</p>
      % if object.media %
      <p> object.media </p>
      <p><a href=" object.get_download ">Download</a></p>
      <p><a href=" object.get_download ?preview=True">Preview</a></p>
      % endif %
      % endblock Content %








      class ProductDownloadlView(MultiSlugMixin,DetailView):
      model=Product
      def get(self,request,*args,**kwargs):
      obj=self.get_object()
      filepath = os.path.join(settings.PROTECTED_ROOT,obj.media.path)
      guessed_type = guess_type(filepath)[0]
      wrapper=FileWrapper(open(filepath))
      mimetype = 'application/force-download'
      if guessed_type:
      mimetype=guessed_type
      response = HttpResponse(wrapper,content_type=mimetype)
      if request.GET.get("preview"):
      response["Content-Disposition"]="attachment; filename=%s" %(obj.media.name)
      response["X-SendFile"]=str(obj.media.name)
      return response





      class ProductDownloadlView(MultiSlugMixin,DetailView):
      model=Product
      def get(self,request,*args,**kwargs):
      obj=self.get_object()
      filepath = os.path.join(settings.PROTECTED_ROOT,obj.media.path)
      guessed_type = guess_type(filepath)[0]
      wrapper=FileWrapper(open(filepath))
      mimetype = 'application/force-download'
      if guessed_type:
      mimetype=guessed_type
      response = HttpResponse(wrapper,content_type=mimetype)
      if request.GET.get("preview"):
      response["Content-Disposition"]="attachment; filename=%s" %(obj.media.name)
      response["X-SendFile"]=str(obj.media.name)
      return response





      % extends "base.html" %
      % block Content %
      <h1>Products</h1>
      <p>object.title </p>
      <p>object.description </p>
      <p>object.price</p>
      % if object.media %
      <p> object.media </p>
      <p><a href=" object.get_download ">Download</a></p>
      <p><a href=" object.get_download ?preview=True">Preview</a></p>
      % endif %
      % endblock Content %





      % extends "base.html" %
      % block Content %
      <h1>Products</h1>
      <p>object.title </p>
      <p>object.description </p>
      <p>object.price</p>
      % if object.media %
      <p> object.media </p>
      <p><a href=" object.get_download ">Download</a></p>
      <p><a href=" object.get_download ?preview=True">Preview</a></p>
      % endif %
      % endblock Content %






      python django imagefield filefield






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 7:04









      CipherCipher

      519 bronze badges




      519 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0














          Django's HttpResponse class is most suitable for a text based response (html page, txt file, etc.). The documentation for the class constructor explains:




          content should be an iterator or a string. If it’s an iterator, it should return strings, and those strings will be joined together to form the content of the response. If it is not an iterator or a string, it will be converted to a string when accessed.




          You UnicodeDecodeError is probably raised when the content is converted to a string. If you want to return a PDF file, the file content is binary data, so it is not intended to be converted to string.



          You may use the FileResponse class instead. It inherits from StreamingHttpResponse, and so have a different APi than HttpResponse, but it will probably works better to return a binary file (like a PDF).






          share|improve this answer

























          • can you suggest where to edit because what i want to return should be a jpg file or just the format of the file i have uploaded

            – Cipher
            Mar 27 at 7:33











          • I think you should return FileRespone instead of HttpResponse. programcreek.com/python/example/86007/django.http.FileResponse

            – Mikey
            Mar 27 at 7:41












          • yeah of course i have used wrapper=FileWrapper(open(filepath,'rb'))

            – Cipher
            Mar 27 at 7:45











          • also response = FileResponse(wrapper,content_type=mimetype) worked i dont know even what am doing with these two options

            – Cipher
            Mar 27 at 7:46










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371531%2funicodedecodeerror-error-charmap-codec-cant-decode-byte-0x81-in-position-250%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Django's HttpResponse class is most suitable for a text based response (html page, txt file, etc.). The documentation for the class constructor explains:




          content should be an iterator or a string. If it’s an iterator, it should return strings, and those strings will be joined together to form the content of the response. If it is not an iterator or a string, it will be converted to a string when accessed.




          You UnicodeDecodeError is probably raised when the content is converted to a string. If you want to return a PDF file, the file content is binary data, so it is not intended to be converted to string.



          You may use the FileResponse class instead. It inherits from StreamingHttpResponse, and so have a different APi than HttpResponse, but it will probably works better to return a binary file (like a PDF).






          share|improve this answer

























          • can you suggest where to edit because what i want to return should be a jpg file or just the format of the file i have uploaded

            – Cipher
            Mar 27 at 7:33











          • I think you should return FileRespone instead of HttpResponse. programcreek.com/python/example/86007/django.http.FileResponse

            – Mikey
            Mar 27 at 7:41












          • yeah of course i have used wrapper=FileWrapper(open(filepath,'rb'))

            – Cipher
            Mar 27 at 7:45











          • also response = FileResponse(wrapper,content_type=mimetype) worked i dont know even what am doing with these two options

            – Cipher
            Mar 27 at 7:46















          0














          Django's HttpResponse class is most suitable for a text based response (html page, txt file, etc.). The documentation for the class constructor explains:




          content should be an iterator or a string. If it’s an iterator, it should return strings, and those strings will be joined together to form the content of the response. If it is not an iterator or a string, it will be converted to a string when accessed.




          You UnicodeDecodeError is probably raised when the content is converted to a string. If you want to return a PDF file, the file content is binary data, so it is not intended to be converted to string.



          You may use the FileResponse class instead. It inherits from StreamingHttpResponse, and so have a different APi than HttpResponse, but it will probably works better to return a binary file (like a PDF).






          share|improve this answer

























          • can you suggest where to edit because what i want to return should be a jpg file or just the format of the file i have uploaded

            – Cipher
            Mar 27 at 7:33











          • I think you should return FileRespone instead of HttpResponse. programcreek.com/python/example/86007/django.http.FileResponse

            – Mikey
            Mar 27 at 7:41












          • yeah of course i have used wrapper=FileWrapper(open(filepath,'rb'))

            – Cipher
            Mar 27 at 7:45











          • also response = FileResponse(wrapper,content_type=mimetype) worked i dont know even what am doing with these two options

            – Cipher
            Mar 27 at 7:46













          0












          0








          0







          Django's HttpResponse class is most suitable for a text based response (html page, txt file, etc.). The documentation for the class constructor explains:




          content should be an iterator or a string. If it’s an iterator, it should return strings, and those strings will be joined together to form the content of the response. If it is not an iterator or a string, it will be converted to a string when accessed.




          You UnicodeDecodeError is probably raised when the content is converted to a string. If you want to return a PDF file, the file content is binary data, so it is not intended to be converted to string.



          You may use the FileResponse class instead. It inherits from StreamingHttpResponse, and so have a different APi than HttpResponse, but it will probably works better to return a binary file (like a PDF).






          share|improve this answer













          Django's HttpResponse class is most suitable for a text based response (html page, txt file, etc.). The documentation for the class constructor explains:




          content should be an iterator or a string. If it’s an iterator, it should return strings, and those strings will be joined together to form the content of the response. If it is not an iterator or a string, it will be converted to a string when accessed.




          You UnicodeDecodeError is probably raised when the content is converted to a string. If you want to return a PDF file, the file content is binary data, so it is not intended to be converted to string.



          You may use the FileResponse class instead. It inherits from StreamingHttpResponse, and so have a different APi than HttpResponse, but it will probably works better to return a binary file (like a PDF).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 7:13









          MikeyMikey

          1615 bronze badges




          1615 bronze badges















          • can you suggest where to edit because what i want to return should be a jpg file or just the format of the file i have uploaded

            – Cipher
            Mar 27 at 7:33











          • I think you should return FileRespone instead of HttpResponse. programcreek.com/python/example/86007/django.http.FileResponse

            – Mikey
            Mar 27 at 7:41












          • yeah of course i have used wrapper=FileWrapper(open(filepath,'rb'))

            – Cipher
            Mar 27 at 7:45











          • also response = FileResponse(wrapper,content_type=mimetype) worked i dont know even what am doing with these two options

            – Cipher
            Mar 27 at 7:46

















          • can you suggest where to edit because what i want to return should be a jpg file or just the format of the file i have uploaded

            – Cipher
            Mar 27 at 7:33











          • I think you should return FileRespone instead of HttpResponse. programcreek.com/python/example/86007/django.http.FileResponse

            – Mikey
            Mar 27 at 7:41












          • yeah of course i have used wrapper=FileWrapper(open(filepath,'rb'))

            – Cipher
            Mar 27 at 7:45











          • also response = FileResponse(wrapper,content_type=mimetype) worked i dont know even what am doing with these two options

            – Cipher
            Mar 27 at 7:46
















          can you suggest where to edit because what i want to return should be a jpg file or just the format of the file i have uploaded

          – Cipher
          Mar 27 at 7:33





          can you suggest where to edit because what i want to return should be a jpg file or just the format of the file i have uploaded

          – Cipher
          Mar 27 at 7:33













          I think you should return FileRespone instead of HttpResponse. programcreek.com/python/example/86007/django.http.FileResponse

          – Mikey
          Mar 27 at 7:41






          I think you should return FileRespone instead of HttpResponse. programcreek.com/python/example/86007/django.http.FileResponse

          – Mikey
          Mar 27 at 7:41














          yeah of course i have used wrapper=FileWrapper(open(filepath,'rb'))

          – Cipher
          Mar 27 at 7:45





          yeah of course i have used wrapper=FileWrapper(open(filepath,'rb'))

          – Cipher
          Mar 27 at 7:45













          also response = FileResponse(wrapper,content_type=mimetype) worked i dont know even what am doing with these two options

          – Cipher
          Mar 27 at 7:46





          also response = FileResponse(wrapper,content_type=mimetype) worked i dont know even what am doing with these two options

          – Cipher
          Mar 27 at 7:46








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371531%2funicodedecodeerror-error-charmap-codec-cant-decode-byte-0x81-in-position-250%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript