asp.net mvc - How to retrieve uploaded file in mvc 4 -
this controller code uploading file , storing title in database , actual file stored in documents/file folder now. facing problem in retrieving uploaded file since have stored title in database. how retrieve file ?
[acceptverbs(httpverbs.post)] public actionresult uploadfile(string title) { _db.uploads.add(new upload() { title = title }); _db.savechanges(); int id = (from in _db.uploads select a.upload_id).max(); if(id>0) { if(request.files["file"].contentlength>0) { string extension = system.io.path.getextension(request.files["file"].filename); string path1 = string.format("{0}/{1}{2}", server.mappath("~/documents/files"), id, extension); if (system.io.file.exists(path1)) system.io.file.delete(path1); request.files["file"].saveas(path1); } viewdata["sucess"] = "success"; } else { viewdata["success"] = "upload failed"; } return view(); }
since have stored title in database
_db.uploads.add(new upload() { title = title }); in _db.uploads select a.upload_id
it appears you're storing id, not title
the save path uses id.
string path1 = string.format("{0}/{1}", server.mappath("~/documents/files"), id);
(i've removed extension now)
so read file using id of file want reload:
[httpget] public actionresult downloadfile(int id) { string path1 = string.format("{0}/{1}", server.mappath("~/documents/files"), id); return filepathresult(path); }
(otomh, untested)
of course, best solution persist filename+extension (no need path if it's same) title , id.
edit: more details:
you store title , id, change store filename path path1
. use retrieve uploaded file.
Comments
Post a Comment