我有一个网页,用户应该使用该网页将 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 中上传和阅读上传文件的内容吗?
最佳答案
解决方案实际上不需要外部方法,我用脚本中的一行来读取上传文件的内容,如下所示;
uploaded_data=context.REQUEST.get('uploaded_file').readlines()
关于mysql - 如何上传文件(csv 或文本)并在 Zope 框架中读取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27574971/