Click or drag to resize

IUserResourceSetImage Method

Set the value of the resource as an image.

Namespace:  AskiaPortalCmn
Assembly:  AskiaPortalCmn (in AskiaPortalCmn.dll) Version: 1.7.0-build068
Syntax
IReturnValue SetImage(
	string fileName,
	byte[] bytes
)

Parameters

fileName
Type: SystemString
Name of the image file (including the extension)
bytes
Type: SystemByte
Array of bytes that represent the image

Return Value

Type: IReturnValue
Return success value if the user in the context have enough permission to do that action.
Remarks
This method will validate the maximum size of the image and the allowed image file extension.
See: AllowedImagesExtensions and MaxImagesFileSizeInKb in the config Config.
Remarks
It will not save the change in the database unless you explicitly call the Save method.
Examples

Set the resource as an image

var httpRequest = HttpContext.Current.Request;  
if (httpRequest.Files.Count != 1)  
{
   throw new Exception("No image or too many image found in the request. The request accept exactly 1 image file.");
}
var imageFile = httpRequest.Files[0];
var binaryReader = new BinaryReader(imageFile.InputStream);
var bytes = b.ReadBytes(imageFile.ContentLength); 
var context = ContextFactory.CreateByLoginOrEmailAndPassword("login", "secret");
var user = context.User;
var resource = user.Avatar;
var result = resource.SetImage(imageFile.FileName, bytes);
if (!result.Success)
{
    throw result.Exception;
}
See Also