asp.net mvc - How to decompress a varbinary file in database to a ZIP file? -
in application, have upload zip file , have make available can download file again. new mvc, have used varbinary store data in database. here view code:
@using (html.beginform("upload", "createnews", formmethod.post, new { enctype = "multipart/form-data" }))
{ @html.antiforgerytoken()
<div class="form-horizontal"> <h4>createnew</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(model => model.categoryid, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.categoryid, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.categoryid, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @*@using (html.beginform("upload", "createnews", formmethod.post, new { enctype = "multipart/form-data" })) {*@ <table> <tr> <td>file:</td> <td> <input type="file" name="uploadedfile" /> </td> </tr> <tr> <td colspan="2"> @*<input type="submit" name="upload" value="submit" />*@ </td> </tr> </table> @*}*@ @html.labelfor(model => model.complete_zip_file, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.complete_zip_file, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.complete_zip_file, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.categoryname, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.categoryname, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.categoryname, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.subcategoryid, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.subcategoryid, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.subcategoryid, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.subcategoryname, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.subcategoryname, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.subcategoryname, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> @*<input type="submit" value="create" class="btn btn-default" />*@<input type="submit" name="upload" value="submit" /> </div> </div> </div>
}
then have written following code in controller: public actionresult upload([bind(include = "categoryid,complete_zip_file,categoryname,subcategoryid,subcategoryname")] createnew createnew) {
if (request != null) { httppostedfilebase file = request.files["uploadedfile"]; if ((file != null) && (file.contentlength > 0) && !string.isnullorempty(file.filename)) { string filename = file.filename; string filecontenttype = file.contenttype; byte[] filebytes = new byte[file.contentlength]; file.inputstream.read(filebytes, 0, convert.toint32(file.contentlength)); createnew.complete_zip_file = filebytes; }} if (modelstate.isvalid) { db.createnews.add(createnew); db.savechanges(); return redirecttoaction("index"); } //return view(createnew) return view("create"); }
now, result getting stored in appropriate field. now, have make available download. so, how can convert varbinary format zip file again?
thanks in advance.
you dont need convert varbinary zip. can directly write response.
public actionresult download() { //read varbinary field db byte[] output = getoutputfromdb(); return file(output, "application/zip", "filename.zip"); }
Comments
Post a Comment