c# - Unable to delete created and streamed file -


i'm using magick.net convert pdf png , stream page via ajax.

everything works until pdf uploaded twice. when trying overwrite or delete existing file, debugger tells me file in use process.

here's function returns image controller:

    //path qualified path file ending in .pdf     private image convertpdftoneimage(string path)     {         magickreadsettings settings = new magickreadsettings();         settings.density = new pointd(300, 300);          using (magickimagecollection images = new magickimagecollection())         {             fileinfo file = new fileinfo(path);             images.read(file);             file = null;              using (magickimage horizontal = images.appendhorizontally())             {                 string pngname = path.changeextension(path, ".png");                 horizontal.write(pngname);             }              return image.fromfile(path.replace("pdf", "png"));         }     } 

and controller streams response browser:

 public async task<httpresponsemessage> postformdata([fromuri] int sellerid, [fromuri] int eventid, [fromuri] string section, [fromuri] string row, [fromuri] string seat)         {             if (httpcontext.current.request.files.allkeys.any())             {                 try                 {                     string base64 = string.empty;                     sellerobjects.externeal.sellerticket tickettosave = new sellerobjects.externeal.sellerticket();                      tickettosave.uploadedfile = httpcontext.current.request.files["uploadedimage"];                     sellerticketrepo thelocalsellerrepo = new sellerticketrepo(tickettosave);                      using (memorystream ms = new memorystream())                     {                         thelocalsellerrepo.getconvertedpdfimage().save(ms, system.drawing.imaging.imageformat.jpeg);                         base64 = system.convert.tobase64string(ms.toarray());                     }                      httpresponsemessage result = new httpresponsemessage(httpstatuscode.ok);                     result.content = new stringcontent(base64);                     result.content.headers.contenttype = new mediatypeheadervalue("image/png");                     return result;                 }                 catch (exception ex)                 {                     return request.createresponse(httpstatuscode.badrequest, "error saving file.");                 }             }              return request.createerrorresponse(httpstatuscode.badrequest, "an error has occurred");         } 

i assuming image class returning system.drawing.image. need dispose object release file lock.

// instead of this: using (memorystream ms = new memorystream()) {   thelocalsellerrepo.getconvertedpdfimage().save(ms, system.drawing.imaging.imageformat.jpeg);   base64 = system.convert.tobase64string(ms.toarray()); }  // should doing this: using (memorystream ms = new memorystream()) {   using (image img = thelocalsellerrepo.getconvertedpdfimage())   {     img.save(ms, system.drawing.imaging.imageformat.jpeg);     base64 = system.convert.tobase64string(ms.toarray());   } }  // or (if getconvertedpdfimage() returns magickimage): using (magickimage img = thelocalsellerrepo.getconvertedpdfimage()) {   img.format = magickformat.jpeg;   base64 = img.tobase64(); } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -