【问题标题】:How to upload a file (csv or text) and read contents in Zope framework如何在 Zope 框架中上传文件(csv 或文本)并读取内容
【发布时间】:2015-02-18 21:59:20
【问题描述】:

我有一个网页,用户应该使用它来将 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 中上传和阅读上传文件内容的经验的人可以帮忙吗?

【问题讨论】:

    标签: mysql csv text upload zope


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2016-03-16
      • 2014-12-08
      • 2021-12-25
      相关资源
      最近更新 更多