i'm using code below downloading web api in asp.net. when i'm trying click download button, calls api. after executing "downloadfile"-function, download dialog box isn't coming .
[httpget]
public httpresponsemessage downloadfile(string downloadfilepath) { httpresponsemessage result = null; var localfilepath = httpcontext.current.server.mappath(downloadfilepath); // check if parameter valid if (string.isnullorempty(downloadfilepath)) { result = request.createresponse(httpstatuscode.badrequest); } // check if file exists on server else if (!file.exists(localfilepath)) { result = request.createresponse(httpstatuscode.gone); } else {// serve file client result = request.createresponse(httpstatuscode.ok); result.content = new streamcontent(new filestream(localfilepath, filemode.open, fileaccess.read)); result.content.headers.contentdisposition = new system.net.http.headers.contentdispositionheadervalue("attachment"); result.content.headers.contentdisposition.filename = downloadfilepath; } return result; }
i didn't exception code above, dialog box downloading file isn't coming.
here code, using , works great. hope give idea
.... var filebytes = helper.getfilebytes(filepath);//convert file bytes var stream = new memorystream(filebytes); resp.content = new streamcontent(stream); resp.content.headers.contenttype = new mediatypeheadervalue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); resp.content.headers.contentdisposition = new contentdispositionheadervalue("attachment") { filename = filerequest.filename }; resp.content.headers.add("content-encoding", "utf-8"); return resp;
and, here code getfilebytes method,
public static byte[] getfilebytes(string filepath) { var fileinfo = new fileinfo(filepath); if (fileinfo.exists) { return file.readallbytes(fileinfo.fullname); } return null; }
Comments
Post a Comment