我跟着这个tutorial但是无法将文件上传到我想要的目录。它说这是成功的,但文件不在它应该的目录中。
更新我更改了我的views.py以尝试修复目录调用,但现在我收到此错误:
File "/home/jsnyder10/Documents/45/app/views.py", line 33, in uploader
f.save(os.path.join(app.config['UPLOAD_FOLDER']),secure_filename(f.filename))
IOError: [Errno 21] Is a directory: '/home/jsnyder10/Documents/45/uploads'
views.py
@app.route('/mobility_import_html')
@login_required
def mobility_import_html():
return render_template('mobility_import_html.html')
@app.route('/uploader', methods = ['GET', 'POST'])
def uploader():
if request.method == 'POST':
f = request.files['file']
#f.save(secure_filename(f.filename))
f.save(os.path.join(app.config['UPLOAD_FOLDER']),secure_filename(f.filename))
print("f.filename", f.filename)
return 'success'
mobility_import_html.html
{% extends "mobility.html" %}
{% block mobility %}
<form action ="{{ url_for('uploader') }}" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
{% endblock %}
初始化的.py
import os
from flask import Flask
from flask.json import JSONEncoder
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_babel import Babel, lazy_gettext
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, \
MAIL_PASSWORD
from .momentjs import momentjs
app.config["UPLOAD_FOLDER"] = os.path.join(basedir, 'uploads')
答案 0 :(得分:0)
您的保存行应该是:
public class ShopFragment extends Fragment {
FragmentManager fm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.i_fragment_shop,container,false);
String[] list1={"Chemicals","Pigments","Uniforms","Safety Products"};
ListView listView= (ListView) rootview.findViewById(R.id.mainlist);
ArrayAdapter<String> listviewadapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list1);
listView.setAdapter(listviewadapter);
fm =getFragmentManager();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
fm.beginTransaction().replace(R.id.content_frame,new Chemical_Fragment()).addToBackStack("shop").commit();
}
if(position==1){
fm.beginTransaction().replace(R.id.content_frame,new Pigments_Fragment()).addToBackStack("shop").commit();
}
if(position==2){
fm.beginTransaction().replace(R.id.content_frame,new Uniforms_Fragment()).addToBackStack("shop").commit();
}
if(position==3){
fm.beginTransaction().replace(R.id.content_frame,new SafetyProducts_Fragment()).addToBackStack("shop").commit();
}
}
});
return rootview;
}
}