c# - Create serverfolder if filepath does not exist -
i have controller method handles file uploads jquery script. specify filepath following code path.combine(server.mappath("~/userfiles/"), qqfile);
add parameter specifies subfolder file, can specify existing folders.
is there easy fix create subfolder on server if doesn't exist?
this controller method:
public actionresult fileupload(string qqfile) { var file = string.empty; try { var stream = request.inputstream; if (string.isnullorempty(request["qqfile"])) { // ie httppostedfilebase postedfile = request.files[0]; stream = postedfile.inputstream; file = path.combine(server.mappath("~/userfiles/"), system.io.path.getfilename(request.files[0].filename)); } else { //webkit, mozilla file = path.combine(server.mappath("~/userfiles/"), qqfile); } var buffer = new byte[stream.length]; stream.read(buffer, 0, buffer.length); system.io.file.writeallbytes(file, buffer); } catch (exception ex) { return json(new { success = false, message = ex.message }, "application/json"); } return json(new { success = true }, "text/html"); }
have tried this?
string path = "~/userfiles/subfolder/"; if (!directory.exists(path)) { directory.createdirectory(path); } //todo
or should work
string folder= "subfolder"; var path= server.mappath("~/userfiles/" + folder); var directory = new directoryinfo(path); if (directory.exists == false) { directory.create(); } //todo
Comments
Post a Comment