Problem: Generally while working with files we might get the below errors
Request for the permission of type 'FileIOPermission' failed.
(S)\Classes\FileIOPermission\demand
(S)\Classes\WinAPIServer\fileExists- line 11
(S)\Classes\WinAPIServer\fileExists- line 11
or
Request for the permission of type 'FileIOPermission' failed.
(S)\Classes\FileIOPermission\demand
(S)\Classes\WinAPIServer\deleteFile- line 11
Solution:
Set permissionSet;
FileName fileName = ''Test.Xml";
permissionSet = new Set(Types::Class);
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(fileName , 'rw'));
CodeAccessPermission::assertMultiple(permissionSet);
if (WINAPIServer::fileExists(fileName))
{
if (Box::yesNo(strfmt("Do you want to re-write the file", filename), DialogButton::No))
{
WINAPIServer::deleteFile(filename);
}
else
{
return;
}
}
CodeAccessPermission::revertAssert();
A copy will also require FileIOPermission of 'r' on the source file. A Move will require a permission of 'w' on the source.
I had the same problem and came up with a similar approach, but I don't use a new parameter in a table/form to get the company logo file, instead I get the bitmap, save it on a temporary disk and then use this file saved on disk to do the magic, follow the enjoy code:
ReplyDelete//<21/02/2023 by Alan Pompilho - C1028797_StmAccountCollectLetters>
ReplyDelete#define.GetImageDimensionUnits("getImageDimensionUnits")
public void PFHWriteBitmapOnServer(OutputBitmapField _field, OuputSection _section)
{
#File
BinData bin = new BinData();
//Image img;
container data, imgInfoData;
str s;
real x1,x2, y1,y2;
Struct br;
int imageObjectNo = 0;
int newwidth, newheight;
int imgWidthUnits, imgHeightUnits;
SysDictClass dc = new SysDictClass(classnum(Image));
real pdfPreviewScale = 0.8;
boolean generateXImage = false;
container c;
str fn;
FileIOPermission writePermission;
FileIOPermission readPermission;
boolean grayScale = false;
System.Drawing.Image img;
str imgStr;
int widthTemp, heightTemp;
CompanyInfo companyInfo = companyInfo::find();
Bitmap logo;
BinData binData = new BinData();
FileName fileNameLogo;
Set permissionSet = new Set(Types::Class);
;
//Creating the temp file path in server disk to logo image
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(fileNameLogo , 'rw'));
CodeAccessPermission::assertMultiple(permissionSet);
fileNameLogo = System.IO.Path::GetTempFileName();
CodeAccessPermission::revertAssert();
//Save the Company logo image in the server path
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(fileNameLogo , 'rw'));
CodeAccessPermission::assertMultiple(permissionSet);
logo = FormLetter::companyLogo();
binData.setData(logo);
binData.saveFile(fileNameLogo);
binData.finalize();
CodeAccessPermission::revertAssert();
new InteropPermission(InteropKind::ClrInterop).assert();
br = this.boundingRectTwips(currentPage, _section, _field);
x1 = br.value('x1'); y1 = br.value('y1');
x2 = br.value('x2'); y2 = br.value('y2');
if (_field.type() == 10) // resourceId, DB_RESId
{
//img = new Image(_field.resourceId());
img = System.Drawing.Image::FromFile(fileNameLogo);
if (resourceIdImageMap.exists(_field.resourceId()))
imageObjectNo = resourceIdImageMap.lookup(_field.resourceId());
else
{
imageObjectNo = this.nextObjectNo();
resourceIdImageMap.insert(_field.resourceId(), imageObjectNo);
generateXImage = true;
}
if (debugLevel >= 1)
info ('Image in resource ' + int2str(_field.resourceId()));
}
else if (_field.type() == 7) // queue
{
c = _field.value();
if (c)
{
//img = new Image(c);
img = System.Drawing.Image::FromFile(fileNameLogo);
imageObjectNo = this.nextObjectNo();
}
generateXImage = true;
if (debugLevel >= 1)
{
if (img)
info ('Image in container');
else
info ('No image in container');
}
}
else // string containing filename
ReplyDelete{
//img = new Image(_field.imageFileName());
img = System.Drawing.Image::FromFile(fileNameLogo);
if (stringImageMap.exists(_field.imageFileName()))
imageObjectNo = stringImageMap.lookup(_field.imageFileName());
else
{
imageObjectNo = this.nextObjectNo();
stringImageMap.insert(_field.imageFileName(), imageObjectNo);
generateXImage = true;
}
if (debugLevel >= 1)
info ('File is ' + _field.imageFileName());
}
if (img)
{
if (generateXImage)
{
fn = System.IO.Path::GetTempFileName();
widthTemp = img.get_Width();
heightTemp = img.get_Height();
img.Save(fn);
// revert previous assertion
CodeAccessPermission::revertAssert();
// assert read permissions
readPermission = new FileIOPermission(fn, #io_read);
readPermission.assert();
// BP deviation documented (note that the file io assert IS included above)
bin.loadFile(fn);
data = bin.getData();
// Get rid of the temporary file.
//WinAPIServer::deleteFile(fn);
CodeAccessPermission::revertAssert();
new InteropPermission(InteropKind::ClrInterop).assert();
System.IO.File::Delete(fn);
if (bitmapEncode85)
s = bin.ascii85Encode();
else
s = BinData::dataToString(data);
objectOffsetMap.insert(imageObjectNo, binBuffer.size());
this.appendTextToBuffer(int2str(imageObjectNo) + ' 0 obj <<\n');
this.appendTextToBuffer(' /Type/XObject /Subtype/Image /ColorSpace /DeviceRGB\n');
this.appendTextToBuffer(' /Name /I' + int2str(imageObjectNo) + ' /BitsPerComponent 8\n');
if (!bitmapEncode85)
{
this.appendTextToBuffer(' /Filter [/ASCIIHexDecode/DCTDecode] /DecodeParms[null<<>>]\n');
this.appendTextToBuffer(strfmt(' /Length %1\n', strlen(s)+1)); // Allow for end of data marker
}
else
{
this.appendTextToBuffer(' /Filter [/ASCII85Decode/DCTDecode] /DecodeParms[null<<>>]\n');
this.appendTextToBuffer(strfmt(' /Length %1\n', strlen(s)));
}
// Specify dimensions via newwidth/newheight.
this.appendTextToBuffer(strfmt(' /Width %1 /Height %2\n', widthTemp, heightTemp));
this.appendTextToBuffer('>>\nstream\n');
this.appendTextToBuffer(s);
if (!bitmapEncode85)
this.appendTextToBuffer('>'); // End of Data marker
this.appendTextToBuffer('\nendstream\nendobj\n');
}
// Have to put imageObjectNo or it will be only on page #1
ReplyDeleteimagesOnPage.add(imageObjectNo);
// Generate the object that draws the image...
s = 'q\n';
s += this.drawFieldFrame(_field, x1, y1, x2, y2);
// Provide a scaling factor bringing the image size up to the size requested by the report
// Also, provide x,y pos)
if (_field.resize())
s += ' ' + this.real2str(x2-x1) + ' 0 0 ' + this.real2str(y2-y1) + ' ' + this.real2str(x1) + ' ' + this.real2str(y1) + ' cm\n';
else
{
//We should resize the bitmap according to the area given in PDF page otherwise there will be out of area drawing
//The PDF bitmap drawing has wrong scaling from what we see in the preview so we should also scale it by PDFPreviewScale
if (dc.hasObjectMethod(#GetImageDimensionUnits))
{
// bp deviation documented
[imgWidthUnits, imgHeightUnits] = dc.callObject(#GetImageDimensionUnits, img);
}
else
{
[imgWidthUnits, imgHeightUnits] = [maxint(), maxint()];
warning(strfmt("@SYS90314", #GetImageDimensionUnits, dc.name()));
}
newwidth = min(abs(x2-x1)/pdfPreviewScale, this.twips(imgWidthUnits));
newheight = min(abs(y2-y1)/pdfPreviewScale, this.twips(imgHeightUnits));
x1 += this.twips(this.borderWidth(_field.borderWidth(), _field.lineLeft()));
y1 += this.twips(this.borderWidth(_field.borderWidth(), _field.lineRight()));
x2 -= this.twips(this.borderWidth(_field.borderWidth(), _field.lineTop()));
y2 -= this.twips(this.borderWidth(_field.borderWidth(), _field.lineBottom()));
if (_field.alignment() == Alignment::Center)
{
x1 = (x1 + x2 - newwidth) / 2;
y1 = (y1 + y2 - newheight) / 2;
}
else if (_field.alignment() == Alignment::Right)
{
x1 = x2 - newwidth;
y1 = y2 - newheight;
}
else // left
{
y1 = y2 - newheight;
}
s += ' ' + this.real2str(newwidth) + ' 0 0 ' + this.real2str(newheight) + ' ' + this.real2str(x1) + ' ' + this.real2str(y1) + ' cm\n';
}
s += ' /I' + int2str(imageObjectNo) + ' Do\n';
s += 'Q\n';
this.appendTextToPage(s);
}