In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the "JS file upload and download, PDF and Excel operation" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
1. File upload front-end code selects files to upload to the server can only upload jpg/png files, and does not exceed 500kb
JS code
New Vue ({el:'# app', data: {fileList: [], actionUrl: site_url + "upload/"...}, mounted () {this.init ()}, methods: {init () {} SubmitUpload () {this.$refs.upload.submit () }, handleRemove (file, fileList) {console.log (file, fileList);}, handlePreview (file) {console.log (file);}, handleExceed (files, fileList) {this.$message.warning (`only 1 file can be uploaded at a time `) }, handleSuccess (res, file, fileList) {this.$message.success ('File uploaded successfully'); this.fileList = [];}, beforeRemove (file, fileList) {return this.$confirm (`deleting uploaded files, do you want to continue? `);}})
When using the el-upload component, there are two upload buttons rendered on the page. You can use CSS to hide the native upload buttons.
Input [type= "file"] {display: none } backend code import osimport timefrom django.views.decorators.csrf import csrf_exemptdef check_upload_wrapper (func): def inner (* args, * * kwargs): if not os.path.exists ("upload/"): os.makedirs ("upload/") return func (* args, * * kwargs) return inner@csrf_exempt # cancel csrf authentication You can not use this decorator @ check_upload_wrapper # decorator, and check whether there is a `upload/ `directory in the background. If not, create def upload_temp (request): file_obj = request.FILES.get ('file') # get the uploaded file object t = time.strftime ('% Y% m% d% H% M% S') now_file_name = t +'.'+ file_obj.name.split ('.') [1] # get the name of the file saved in the background file_path = os.path.join ('upload') Now_file_name) with open (file_path, "wb") as f: for line in file_obj.chunks (): f.write (line) return JsonResponse ({'result': True,' data': file_path}) # must return the file save path 2. File download frontend code downloads new Vue ({el:'# home', data: {}, mounted () {this.init ()}, methods: {init () {}) Download () {location.href = site_url + 'download/'}) backend code from django.utils.encoding import escape_uri_pathfrom django.http import HttpResponsedef download (request): file_name = u "merge .pdf" file = open (file_name) 'rb') response = HttpResponse (file) response [' Content-Type'] = 'application/octet-stream' response [' Content-Disposition'] = "attachment The filename*=utf-8'' {} ".format (escape_uri_path (file_name)) return response3.PDF file merges pdffile1 = open (ringing file1.pdfills, 'rb') pdffile2 = open (ritual file2.pdfills,' rb') pdf1_reader = PyPDF2.PdfFileReader (pdffile1) pdf2_reader = PyPDF2.PdfFileReader (pdffile2) # to create a pdf document, which only represents the value of the pdf document and does not create the actual document. Pdf_writer = PyPDF2.PdfFileWriter () # read the document page by page into a new document for pagenum in range (pdf1_reader.numPages): pageobj = pdf1_reader.getPage (pagenum) pdf_writer.addPage (pageobj) for pagenum in range (pdf2_reader.numPages): pageobj = pdf2_reader.getPage (pagenum) pdf_writer.addPage (pageobj) # write method can actually generate a file pdfoutputfile = open (u' merge .pdf') 'wb') pdf_writer.write (pdfoutputfile) pdfoutputfile.close () pdffile1.close () pdffile2.close () 4. Read Excel file # read Excel file def read_excel (): # add r before the path, read file path file_path = rroomfile1.xlsx' # Chinese transcoding of file path file_path = file_path.decode ('utf-8') # get data data = xlrd.open_workbook (file_path) # get sheet Usually Sheet1 table = data.sheet_by_name (u'Sheet1) # get the total number of lines of the excel file nrows = table.nrows # read the data for i in range (1, nrows) from the second line: # read the data of the first column of each row value1 = table.cell (I, 0). Value.strip () # read the data of the second column of each row value2 = table.cell (I 1. Value.strip () 5. Write Excel file 5.1 xlwt module writes Excel file def write_excel (sheet_name, titles, col1, col2): f = xlwt.Workbook () # add a Sheet Parameter sheet1 = f.add_sheet (sheet_name, cell_overwrite_ok=True) # write header for i in range (0, len (titles)): # I indicates column I of the first line sheet1.write (0, I, titles [I]) # write data for i in range (0) from the second line Len (col1): # write data sheet1.write (I + 1,0, col1 [I]) to the first column of each row # write data sheet1.write (I + 1,1, col2 [I]) to the second column of each row # the first parameter represents the row Calculate from 0 # the second parameter represents the column Calculated from 0 # the second parameter represents the written data # sheet1.write (1, 3) ) # first parameter: rows at the beginning of the merge # second parameter: rows at the end of the merge (multiple rows can be merged at once) # third parameter: columns at the beginning of the merge # fourth parameter: columns at the end of the merge (multiple rows and columns can be merged at a time) # fifth parameter: data written sheet1.write_merge (1, 3, 3, 3 ) # merge column cells sheet1.write_merge (4, 10, 3, 4, u 'basketball') f.save ('% s.xls'% sheet_name) 5.2 xlsxwriter module writes Excel file def write_excel1 (): # create a new file The file name is: hello.xlsx workbook = xlsxwriter.Workbook ('hello.xlsx') # create sheet You can pass in parameters to specify sheet name worksheet = workbook.add_worksheet (u "Ren life") titles = [u "name", u "age", u "date of birth", u "hobby"] col1 = [u "Zhang San", u "Li Si", u "love Python", u "Xiaoming", u "Xiao Hong", u "nameless"] col2 = [12, 13, 14, 15, 16 17] # write file header for i in range (len (titles)): worksheet.write (0, I, titles [I]) # write file contents for j in range (len (col1)): # write data worksheet.write (j + 1,0) to the first column of each line starting from the second line Col1 [j]) # writes data to the second column of each row from the second row worksheet.write (j + 1,1, col2 [j]) workbook.close () "JS implements file upload and download This is the end of the introduction to the operation of PDF and Excel. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.