我不希望文件保存在我的服务器上,我只想在下一页中读取和打印文件。现在我有了这个。
(index.html)
<form name="fileUpload" method="post">
<input type="file" />
<input type="submit" value="Submit" />
</form>
我试图做这样的事情 -
def upload_file(request):
if request.method == "POST":
upload = request.POST.get('fileUpload').read()
return render(request, 'directory/return.html', {'output': upload})
else:
return render(request, 'directory/index.html')
但显然这不起作用。我希望它适用于文本文件和csv文件。
谢谢。
答案 0 :(得分:6)
首先,您的表单中缺少一些您必须添加的内容。
要使用表单上传文件,您需要在0 swift 0x0000000104300edb llvm::sys::PrintStackTrace(__sFILE*) + 43
1 swift 0x000000010430161b SignalHandler(int) + 379
2 libsystem_platform.dylib 0x00007fff9adeef1a _sigtramp + 26
3 swift 0x00000001041a157e llvm::StringMap<llvm::Value*, llvm::MallocAllocator>::insert(std::__1::pair<llvm::StringRef, llvm::Value*>) + 142
4 swift 0x000000010415bfdc llvm::StoreInst::StoreInst(llvm::Value*, llvm::Value*, bool, llvm::Instruction*) + 60
5 swift 0x0000000103e57b44 llvm::IRBuilder<true, llvm::ConstantFolder, llvm::IRBuilderDefaultInserter<true> >::CreateStore(llvm::Value*, llvm::Value*, bool) + 68
6 swift 0x0000000102506949 swift::irgen::SingleScalarTypeInfo<(anonymous namespace)::PrimitiveTypeInfo, swift::irgen::LoadableTypeInfo>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 41
7 swift 0x00000001024fa0bc swift::irgen::SequentialTypeInfo<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 220
8 swift 0x00000001024fa0bc swift::irgen::SequentialTypeInfo<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 220
9 swift 0x00000001024f89cc swift::irgen::SequentialTypeInfo<(anonymous namespace)::ClangRecordTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::ClangFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 236
10 swift 0x00000001024f89cc swift::irgen::SequentialTypeInfo<(anonymous namespace)::ClangRecordTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::ClangFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 236
11 swift 0x0000000102498c5f swift::irgen::CallEmission::setArgs(swift::irgen::Explosion&, llvm::ArrayRef<swift::SILParameterInfo>, swift::irgen::WitnessMetadata*) + 1775
12 swift 0x00000001024c0f79 swift::irgen::emitObjCPartialApplication(swift::irgen::IRGenFunction&, swift::SILDeclRef, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Value*, swift::SILType, swift::irgen::Explosion&) + 1945
13 swift 0x000000010252eec4 swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 32084
14 swift 0x0000000102523c1d swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 10973
15 swift 0x000000010248b938 swift::irgen::IRGenModuleDispatcher::emitGlobalTopLevel() + 376
16 swift 0x000000010250af39 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 793
17 swift 0x000000010250b3b0 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 64
18 swift 0x00000001024128a8 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&) + 13704
19 swift 0x000000010240f10a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2682
20 swift 0x000000010240b797 main + 2247
21 libdyld.dylib 0x00007fff994545c9 start + 1
22 libdyld.dylib 0x000000000000004f start + 1723513479
元素中将enctype
定义为"multipart/form-data"
。此外,文件输入元素中应包含<form>
属性
<强>的index.html 强>
name
然后,在您的视图中,您可以使用request.FILES
字典访问上传的文件。根据{{1}}文档:
<form enctype="multipart/form-data" action="/my/url/" method="post"> # define the enctype <input type="file" name="my_uploaded_file"/> # define a 'name' attribute <input type="submit" value="Submit" /> </form>
中的每个键都是request.FILES
中的名称。FILES
中的每个值都是<input type="file" name="" />
。
您可以使用FILES
词典中的UploadedFile
键访问上传的文件。
<强> views.py 强>
my_uploaded_file
注意:强>
如果请求方法为request.FILES
,
def upload_file(request): if request.method == "POST": my_uploaded_file = request.FILES['my_uploaded_file'].read() # get the uploaded file # do something with the file # and return the result else: return render(request, 'directory/index.html')
将仅包含数据 并且发布请求的request.FILES
具有该属性POST
。否则,<form>
将为空。
答案 1 :(得分:0)
首先,您必须更改表单:
<form method="post" action="/your/view/url/" enctype="multipart/form-data">
<input name="testing_file" type="file" />
<input type="submit" value="Submit" />
</form>
接下来,您将收到文件:
request.FILES['testing_file']
答案 2 :(得分:0)
第一个答案(Rahul Gupta 的)对我来说效果很好!但这是我遇到的一些错误(我是新手,但也许这可以帮助某人)
**(TypeError: upload_file() missing 1 required positional argument: 'request')**
为了解决它(并且因为我正在处理路由),在最顶端,我没有使用:def upload_file(request):
但是:
@app.route("/", methods=["GET", "POST"])
def index():
**AttributeError: 'NoneType' object has no attribute 'FILES'**
为了解决这个问题,我意识到我实际上无法使用 request.files
函数,因为我忘记安装请求库(并且因为我不得不升级我的 PIP 版本而遇到了一些麻烦),但我终于做了,然后我不得不导入 requests
模块。
这是指导我完成此操作的链接,因为我无法更好地解释它:https://www.pythonforbeginners.com/requests/using-requests-in-python
**AttributeError: 'NoneType' object has no attribute 'FILES'** (Again!!!)
我以为我发现了这个错误,但它又回来了!在阅读了许多不同的网站后,我意识到 requests.FILES
不起作用,而是 requests.files
(没有大写!)。我不确定这是否是更新,但很多人都遇到了这个问题。
希望这有帮助!!! :)