Exemple #1
0
def upload_pic(request):
    make_sub_path(pdf_path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
    pic = request.POST.get('imgData', '')
    field_id = request.POST.get('field_id', '')
    file_url = ''
    if pic != '':
        pic_ext = pic.split(';')[0]
        pic_ext = pic_ext.split('/')[-1]
        pic_data = pic.split(';')[1]
        pic_data = pic_data.split(',')[-1]

        save_name = '%s_%s.%s' % (field_id, time.time(), pic_ext)
        file_path = os.path.join(pdf_path, save_name)

        data = base64.b64decode(pic_data)
        f = open(file_path, 'wb')
        f.write(data)
        f.flush()
        f.close()

        file_url = '%s/%s/%s' % (fs_url, 'pdf', save_name)

    s = """
        {
        "errcode":0,
        "errmsg":"保存成功",
        "url":"%s"
        }
        """ % (file_url)
    return HttpResponseCORS(request, s)
Exemple #2
0
def savePic(pic_data, pic_path, year, filename):
    path = os.path.join(upload_path, str(year))
    make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
    path = os.path.join(path, str(pic_path))
    make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path

    file_path = os.path.join(path, filename)
    data = base64.b64decode(pic_data)
    f = open(file_path, 'wb')
    f.write(data)
    f.flush()
    f.close()
    return
def editor_attach_save(request):
    #print request.POST
    ext_arr = {
        'image': ('gif', 'jpg', 'jpeg', 'png', 'bmp'),
        'flash': ('swf', 'flv'),
        'media': ('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi',
                  'mpg', 'asf', 'rm', 'rmvb'),
        'file': ('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt',
                 'zip', 'rar', 'gz', 'bz2', '7z', 'pdf'),
    }
    today = datetime.date.today()
    year = today.year
    month = today.month
    if request.method == "POST":  # 请求方法为POST时,进行处理
        usr_id = request.session.get('usr_id', 0)
        myFile = request.FILES.get("imgFile", None)  # 获取上传的文件,如果没有文件,则默认为None
        if not myFile:
            return alert(request, '没有找到上传的文件!')
        title = myFile.name
        name_ext = title.split('.')[-1].lower()  #后缀名
        dir_name = request.GET.get('dir', '').strip()  #文件目录
        #检查目录名
        dirName = ext_arr.get(dir_name)

        if dir_name == '':
            for e in ['image', 'flash', 'media', 'file']:
                v = ext_arr[e]
                if name_ext in v:
                    dir_name = e
                    dirName = ext_arr.get(dir_name)
                    break
            else:
                return alert(request, '上传文件扩展名不正确.')
        elif name_ext not in dirName:
            return alert(request, '上传文件扩展名不正确.')

        fname = "%s_%s_%s.%s" % (usr_id, time.time(), random.randint(
            0, 99), name_ext)
        make_sub_path(editor_path)
        path = os.path.join(editor_path, str(usr_id))
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        path = os.path.join(path, dir_name)
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        destination = open(os.path.join(path, fname),
                           'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
        destination.close()
        file_url = '%s/%s/%s/%s/%s' % (fs_url, 'editor_files', usr_id,
                                       dir_name, fname)
        s = """{      
            "error":0,             
            "url":"%s",
            }
            """ % (file_url)
        #print s
        return HttpResponseJsonCORS(request, s)

    return alert(request, '未知错误.')
Exemple #4
0
def attach_save(request):
    #print request.POST
    today = datetime.date.today()
    year = today.year
    month = today.month
    if request.method == "POST":  # 请求方法为POST时,进行处理
        pk = request.POST.get('pk', '')
        random_no = request.POST.get('random_no', '')
        usr_id_gy = request.session.get('usr_id_gy', '') or testid
        # file_type = request.POST.get('file_type', '')
        if usr_id_gy == 0:
            s = """
            {
            "errcode": -1,
            "errmsg": "无权访问,请先关注"
            }        """
            return HttpResponseJsonCORS(s)
        myFile = request.FILES.get("file", None)  # 获取上传的文件,如果没有文件,则默认为None
        if not myFile:
            s = """
                {
                "error": true
                }
                """
            return HttpResponseJsonCORS(s)
        title = myFile.name
        f_ext = title.split('.')[-1]
        fname = "gy_%s_%s_%s.%s" % (usr_id_gy, time.time(),
                                    random.randint(0, 99), f_ext)

        make_sub_path(upload_path)
        path = os.path.join(upload_path, str(year))
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        path = os.path.join(path, str(month))
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        destination = open(os.path.join(path, fname),
                           'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
        destination.close()

        import imghdr
        imgType = imghdr.what(os.path.join(path, fname))
        if imgType in [
                'rgb', 'gif', 'pbm', 'pgm', 'ppm', 'tiff', 'rast', 'xbm',
                'jpeg', 'bmp', 'png'
        ]:
            is_pic = 1
        else:
            is_pic = 0
        if pk == '': pk = 'NULL'
        sql = """insert into file_pic_gy (title,fname,file_size,is_pic,random_no,cid,ctime,file_type)
                    values('%s','%s',%s,%s,'%s',%s,now(),'tstj');
              """ % (title, fname, myFile.size, is_pic, random_no, usr_id_gy)
        #print sql
        db.executesql(sql)

        if is_pic == 1:
            img = Image.open(os.path.join(path, fname))
            x, y = img.size
            if x > 80:
                x1 = 80
                y1 = 80 * y / x
            else:
                x1 = x
                y1 = y
            img = img.resize((x1, y1), Image.ANTIALIAS)
            path = os.path.join(path, 'thumbnail')
            make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
            img.save(os.path.join(path, fname))
            pic_url = os.path.join(front_url, str(year), str(month),
                                   'thumbnail', fname)
        else:
            pic_url = ""
        url = os.path.join(front_url, str(year), str(month), fname)

        s = """{"files":[{        
            "error":false,             
            "size":%s,
            "name":"%s",
            "thumbnail_url":"%s",
            "url":"%s/fileUpload_gy/file_down?fname=%s",
            "delete_url":"%s/fileUpload_gy/del_attach_file?fname=%s"
            }]}
            """ % (myFile.size, myFile.name, pic_url, data_url, fname,
                   data_url, fname)
        return HttpResponseJsonCORS(s)

    s = """
        {
        "error": true
        }
        """
    return HttpResponseJsonCORS(s)
def attach_save(request):
    today = datetime.date.today()
    year = today.year
    month = today.month
    if request.method == "POST":  # 请求方法为POST时,进行处理
        menu_id = request.POST.get('menu_id', 0)
        id = request.POST.get('pk', '')
        source = request.POST.get('option', '')
        if source == '': source = 1
        random_no = request.POST.get('random_no', '')
        ret, d_value = check_usr(request)
        if ret != 0:
            return HttpResponseCORS(request, errmsg)
        usr_id = d_value[0]
        usr_name = d_value[1]
        myFile = request.FILES.get("file", None)  # 获取上传的文件,如果没有文件,则默认为None
        if not myFile:
            s = """
                {
                "error": 1
                }
                """
            return HttpResponseJsonCORS(request, s)
        title = myFile.name
        f_ext = title.split('.')[-1]
        fname = "%s_%s_%s.%s" % (usr_id, time.time(), random.randint(
            0, 99), f_ext)
        small_name = "small_%s" % (fname)
        if f_ext.upper() in ['GIF', 'JPG', 'JPEG', 'PNG', 'BMP']:
            is_pic = 1
        else:
            is_pic = 0
        if id == '': id = 'NULL'
        sql = """insert into file_pic (menu_id,gw_id,title,fname,file_size,is_pic,random_no,cid,cusrname,ctime,source)
                    values(%s,%s,'%s','%s',%s,%s,'%s',%s,'%s',now(),%s);
              """ % (menu_id, id, title, fname, myFile.size, is_pic, random_no,
                     usr_id, usr_name, source)
        #print sql
        db.executesql(sql)
        sql = "select last_insert_id();"
        rows, iN = db.select(sql)
        file_id = rows[0][0]

        make_sub_path(upload_path)
        path = os.path.join(upload_path, str(year))
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        path = os.path.join(path, str(month))
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        destination = open(os.path.join(path, fname),
                           'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
        destination.close()

        import imghdr
        imgType = imghdr.what(os.path.join(path, fname))
        if imgType in [
                'rgb', 'gif', 'pbm', 'pgm', 'ppm', 'tiff', 'rast', 'xbm',
                'jpeg', 'bmp', 'png'
        ]:
            is_pic = 1
        else:
            is_pic = 0
        sql = 'update file_pic set is_pic=%s where id=%s' % (is_pic, file_id)
        db.executesql(sql)
        if is_pic == 1:
            img = Image.open(os.path.join(path, fname))
            x, y = img.size
            x1 = 80
            y1 = 80 * y / x
            try:
                img = img.resize((x1, y1), Image.ANTIALIAS)
                img.save(os.path.join(path, small_name))
                pic_url = os.path.join(front_url, 'attach', str(year),
                                       str(month), small_name)
            except:
                pic_url = ""
        else:
            pic_url = ""
        url = os.path.join(front_url, 'attach', str(year), str(month), fname)
        s = """{"files":[{        
            "error":false, 
            "file_id":%s,            
            "size":%s,
            "name":"%s",
            "thumbnail_url":"%s",
            "url":"%s",
            "delete_url":"%s/del_file/?fname=%s"
            }]}
            """ % (file_id, myFile.size, myFile.name, pic_url, url, data_url,
                   fname)
        return HttpResponseJsonCORS(request, s)

    s = """
        {
        "error": 2
        }
        """
    return HttpResponseJsonCORS(request, s)
def file_manage_json(request):  #kindeditor
    ext_arr = {
        'image': ('gif', 'jpg', 'jpeg', 'png', 'bmp'),
        'flash': ('swf', 'flv'),
        'media': ('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi',
                  'mpg', 'asf', 'rm', 'rmvb'),
        'file': ('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt',
                 'zip', 'rar', 'gz', 'bz2', 'pdf'),
    }
    usr_id = request.session.get('usr_id', 0)
    PATH = os.path.join(editor_path, str(usr_id))
    make_sub_path(PATH)

    dir_name = request.POST.get('dir', '').strip()
    goPath = request.POST.get('path', '').strip()  #上级目录
    if dir_name and dir_name not in ext_arr:
        return "Invalid Directory name."

    if goPath:
        go = goPath.split('/')[1]
        if goPath in ('.', '/', './', '', '../'):
            pass
        elif goPath in ext_arr:
            dir_name = goPath
            PATH = os.path.join(PATH, goPath)
        elif go in ext_arr:
            dir_name = go
            PATH = os.path.join(PATH, go)

    elif dir_name:
        PATH = os.path.join(PATH, dir_name)
        make_sub_path(PATH)
    #
    file_list = []
    i = 0
    for e in os.listdir(PATH):
        if e[0] == '.': continue
        mypath = os.path.join(PATH, e)
        if not os.path.isfile(mypath):
            d = {
                'is_dir': True,
                'has_file': len(os.listdir(mypath)),
                'filesize': 0,
                'is_photo': False,
                'filetype': ''
            }
        else:
            file_ext = e.split('.')[-1]
            d = {
                'is_dir': False,
                'has_file': False,
                'filesize': os.path.getsize(mypath),
                'dir_path': '',
                'is_photo': file_ext in ext_arr['image'],
                'filetype': file_ext
            }

        t = time.strftime("%Y-%m-%d %H:%M:%S",
                          time.localtime(os.path.getmtime(mypath)))
        d.update({'filename': e, 'datetime': t})
        i += 1
        file_list.append(d)

    path = form.get('path')
    order = form.get('order', '').lower() or 'name'
    d_order['order'] = order
    sorted(file_list, cmp=cmp_func)
    file_url = '%s/%s/%s/%s' % (fs_url, 'editor_files', usr_id, dir_name)
    result = {
        'moveup_dir_path': './',
        'current_dir_path': '/',
        'current_url': file_url,
        'total_count': len(file_list),
        'file_list': file_list
    }
    return HttpResponseJsonCORS(request, result)
def upload_licence(request):
    if request.method == "POST":    # 请求方法为POST时,进行处理  
        file_type = request.POST.get('file_type', '1')
        random_no = request.POST.get('random_no', '')
        if str(file_type) == '7':
            usr_id_gy = request.session.get('usr_id_qy','')
            upload_path1 = upload_path_mat
            front_url1 = front_url_mat
        else:
            usr_id_gy = request.session.get('usr_id_gy','') or testid
            upload_path1 = upload_path
            front_url1 = front_url

        if usr_id_gy ==0:
            s = """
            {
            "errcode": -1,
            "errmsg": "无权访问,请先关注"
            }        """
            return HttpResponseJsonCORS(s)
        myFile =request.FILES.get("file", None)    # 获取上传的文件,如果没有文件,则默认为None  
        if not myFile:  
            s = """
                {
                "error": true
                }
                """
            return HttpResponseJsonCORS(s)
        title = myFile.name
        f_ext=title.split('.')[-1]
        fname = "%s_%s_%s.%s"%(usr_id_gy,time.time(),random.randint(0,99),f_ext)

        path=os.path.join(upload_path1,str(usr_id_gy))
        make_sub_path(path) #检查目录是否存在,如果不存在,生成目录  make_sub_path
        destination = open(os.path.join(path,fname),'wb+')    # 打开特定的文件进行二进制的写操作  
        for chunk in myFile.chunks():      # 分块写入文件  
            destination.write(chunk)  
        destination.close()  

        if str(file_type) == '1':
            title = u'营业执照.%s'%(f_ext)
        elif str(file_type) == '2':
            title = u'组织机构代码证.%s'%(f_ext)
        elif str(file_type) == '3':
            title = u'税务登记证.%s'%(f_ext)
        elif str(file_type) == '4':
            title = u'身份证正面.%s'%(f_ext)
        elif str(file_type) == '5':
            title = u'身份证反面.%s'%(f_ext)

        import imghdr
        imgType = imghdr.what(os.path.join(path,fname))
        if imgType in['rgb','gif','pbm','pgm','ppm','tiff','rast','xbm','jpeg','bmp','png']:
            is_pic = 1
        else:
            is_pic=0

        if str(file_type) in ['1','2','3','4','5']:
            sql="SELECT ifnull(fname,'') FROM suppliers_pic WHERE cid = %s and file_type=%s"%(usr_id_gy,file_type)
            lT,iN=db.select(sql)
            if iN>0:
                L=list(lT[0])
                fname1=L[0]
                path1=os.path.join(upload_path1,str(usr_id_gy))
                if fname1!='':
                    path1=os.path.join(path1,fname1)
                    try:
                        os.remove(path1)
                    except:
                        pass                        
                sql="delete from suppliers_pic WHERE cid=%s and file_type=%s"%(usr_id_gy,file_type)
                db.executesql(sql)

        sql = """insert into suppliers_pic (title,fname,file_size,cid,ctime,file_type,is_pic,random_no)
                    values('%s','%s',%s,%s,now(),%s,%s,'%s');
              """%(title,fname,myFile.size,usr_id_gy,file_type,is_pic,random_no)
        #print sql
        db.executesql(sql)

        if is_pic == 1: 
            try:
                img = Image.open(os.path.join(path,fname))
                x,y = img.size
                if x>500:
                    x1 = 500
                    y1 = 500*y/x
                else:
                    x1 = x
                    y1 = y
                img = img.resize((x1, y1), Image.ANTIALIAS)
                path = os.path.join(path,'thumbnail')
                make_sub_path(path) #检查目录是否存在,如果不存在,生成目录  make_sub_path
                img.save(os.path.join(path,fname))
                pic_url = os.path.join(front_url1,str(usr_id_gy),'thumbnail',fname)
            except:
                pic_url=""
        else: 
            pic_url=""
        url = os.path.join(front_url1,str(usr_id_gy),fname)

        s = """{"files":[{        
            "error":false,             
            "size":%s,
            "name":"%s",
            "thumbnail_url":"%s",
            "url":"%s",
            "delete_url":"%s/del_attach_file/?fname=%s"
            }]}
            """%(myFile.size,myFile.name,pic_url,url,data_url,fname)
        print s
        return HttpResponseJsonCORS(s)

    s = """
        {
        "error": true
        }
        """
    return HttpResponseJsonCORS(s)
def attach_save_qy(request):
    #print request.POST
    today = datetime.date.today()
    year = today.year
    month = today.month
    if request.method == "POST":  # 请求方法为POST时,进行处理
        pk = request.POST.get('pk', '')
        random_no = request.POST.get('random_no', '')
        usr_id_qy = request.session.get('usr_id_qy', '') or testid
        file_type = ''
        btntype = request.POST.get('btnType', '') or 'NULL'
        # print request.POST
        if str(btntype) == '0':
            file_type = 'jb'
        if str(btntype) == '-1':
            file_type = 'sbld'
        if str(btntype) == '1':
            file_type = 'tjsl'
        if str(btntype) == '2':
            file_type = 'jgsb'
        if str(btntype) == '3':
            file_type = 'jgqr'
        if str(btntype) == '4':
            file_type = 'jsz'
        if usr_id_qy == 0:
            s = """
            {
            "errcode": -1,
            "errmsg": "无权访问,请先关注"
            }        """
            return HttpResponseJsonCORS(s)
        myFile = request.FILES.get("file", None)  # 获取上传的文件,如果没有文件,则默认为None
        # print myFile
        if not myFile:
            s = """
                {
                "error": true
                }
                """
            return HttpResponseJsonCORS(s)
        title = myFile.name
        f_ext = title.split('.')[-1]
        fname = "qy_%s_%s_%s.%s" % (usr_id_qy, time.time(),
                                    random.randint(0, 99), f_ext)
        if f_ext.upper() in ['GIF', 'JPG', 'PNG', 'BMP']:
            is_pic = 1
        else:
            is_pic = 0
        if pk == '': pk = 'NULL'
        sql = """insert into file_pic_lw (m_id,title,fname,file_size,is_pic,random_no,cid,ctime,file_type,file_type_id)
                    values(%s,'%s','%s',%s,%s,'%s',%s,now(),'%s',%s);
              """ % (pk, title, fname, myFile.size, is_pic, random_no,
                     usr_id_qy, file_type, btntype)
        # print sql
        db.executesql(sql)
        make_sub_path(upload_path)
        path = os.path.join(upload_path, str(year))
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        path = os.path.join(path, str(month))
        make_sub_path(path)  #检查目录是否存在,如果不存在,生成目录  make_sub_path
        destination = open(os.path.join(path, fname),
                           'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
        destination.close()
        # if is_pic == 1: pic_url = "%s/get_file/?fname=%s"%(data_url,fname)
        # else: pic_url=""
        # s = """{"files":[{
        #     "error":false,
        #     "size":%s,
        #     "fname":"%s"
        #     }]}
        #     """%(myFile.size,fname)

        if is_pic == 1:
            pic_url = "%s/fileUpload/file_down?fname=%s" % (data_url, fname)
        else:
            pic_url = ""
        s = """{"files":[{        
            "error":false,             
            "size":%s,
            "name":"%s",
            "thumbnail_url":"%s",
            "url":"%s/fileUpload/file_down?fname=%s",
            "delete_url":"%s/fileUpload/del_attach_file?fname=%s"
            }]}
            """ % (myFile.size, myFile.name, pic_url, data_url, fname,
                   data_url, fname)
        return HttpResponseJsonCORS(s)

    s = """
        {
        "error": true
        }
        """
    return HttpResponseJsonCORS(s)