如何在Zope框架中上传文件(csv或文本)和阅读内容

时间:2014-12-19 22:48:28

标签: mysql csv text upload zope

我有一个用户应该用来将CSV内容上传到MySQL数据库的网页,如何使用外部方法获取内容并将其插入数据库?

首先,我有一个用户用来上传文件的表单:

<form role="form" action="scripts/processUpload" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <input type="file" class="form-control" name="file_name" id="file_name" accept=".csv,.txt">
        <input type="hidden" name="type_of_upload" value="cell_line">
    </div>
    <button type="submit" class="btn btn-primary" style="align-content:center" id="i_submit">Upload</button>
    <script>
        $('#i_submit').click(function () {
            //check whether browser fully supports all File API
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                //get the file size and file type from file input field
                var fsize = $('#file_name')[0].files[0].size;
                var ftype = $('#file_name')[0].files[0].type;
                var fname = $('#file_name')[0].files[0].name;

                if (fsize > 5242880) //do something if file size more than 5 mb (1048576)
                {
                    alert("Type :" + ftype + " | " + fsize + " bites\n(File: " + fname + ") Too big!");
                }
            } else {
                alert("Please upgrade your browser, because your current browser lacks some new features we need!");
            }
        });

        $('#i_submit').click(function () {
            //check whether browser fully supports all File API
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                //get the file size and file type from file input field
                var fsize = $('#file_name')[0].files[0].size;
                var ftype = $('#file_name')[0].files[0].type;
                var fname = $('#file_name')[0].files[0].name;
                //alert(ftype)
                switch (ftype) {
                    case 'application/vnd.ms-excel':
                    case 'text/plain':
                        //alert("Acceptable image file!");
                        break;
                    default:
                        alert('Unsupported File format!');
                }

            } else {
                alert("Please upgrade your browser, because your current browser lacks some new features we need!");
            }
        });
    </script>
</form>

procesUpload脚本的一部分如下所示,我正在使用request来获取文件:

data=context.fileReader(data_file=context.REQUEST.file_name.name)
print data
return printed

fileReader是一个外部方法,它接收数据文件并且应该读取内容:

def readFiles(data_file):
    with open(data_file,'r') as f:
        data=f.readlines()
        for line in data:
            words = line.split(",")
            data_read.append(words)
    return data_read

但是在完成所有这些之后,我得到了:

Error Type: IOError
Error Value: (13, 'Permission denied'),

具有如何上传和阅读Zope上传文件内容经验的人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

解决方案实际上并不需要外部方法,我在脚本中用一行来读取上传文件的内容,如下所示;

uploaded_data=context.REQUEST.get('uploaded_file').readlines()