This is what I am looking for! I want to return the "500 - internal error" to client during development/staging, because not all developers has right to access host server to get the log file.
I followed ismcagdas' example to set "SendAllExceptionsToClients = true;" in SNetWebHostModule. It works, but I get the whole stacks message which is hard to read. I want to filter out some key words and convert to a brief, easy to understand message. So, I tried following suggestion of aaron and alper by adding a class into *Web.Host/Startup, but there is no different. I still get the whole stacks messages. Here is my code:
namespace *.Web.Startup
{
public class ExceptionFilter : IExceptionFilter, IExceptionToErrorInfoConverter, ITransientDependency
{
public IExceptionToErrorInfoConverter Next { set => throw new NotImplementedException(); }
public ErrorInfo Convert(Exception exception)
{
return new ErrorInfo(exception.Message.Substring(0, 20));
}
public void OnException(ExceptionContext context)
{
if (context.Exception is UserFriendlyException)
{
return;
}
context.Exception = new UserFriendlyException("We could not service your request at this stage, please try again later", context.Exception);
}
}
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//MVC
services.AddMvc(options =>
{
options.Filters.Add(new CorsAuthorizationFilterFactory(DefaultCorsPolicyName));
options.Filters.AddService(typeof(ExceptionFilter));
});
What did I miss?
Thanks,
Solved. I wrongly understand the "FileToken". It is used as an encrypted file name, instead of a directory.
Just change the code:
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
File.Copy(fileSource, Path.Combine(filePath, fileName));
as
File.Copy(fileSource, filePath);
It solved my issue.
Now, my next questions:
Thanks,
I figured out my first question, but not 100%. Here is my tests:
I like to keep as simple as possible at my first test. I created a method in FileController:
public ActionResult DownloadTestFile()
{
var filePath = @"C:\x.xlsx";
if (!System.IO.File.Exists(filePath))
{
throw new UserFriendlyException(L("RequestedFileDoesNotExists"));
}
var fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
It works fineas I use a browser and url: <a class="postlink" href="http://localhost:22742/File/DownloadTestFile">http://localhost:22742/File/DownloadTestFile</a>.
Now, I want to add a fileToken on it. I created a service:
public class FileAppService : *AppServiceBase, IImageAppService
{
private readonly IAppFolders _appFolders;
public FileAppService(IAppFolders appFolders)
{
_appFolders = appFolders;
}
public FileDto GetFile()
{
var fileSource = @"C:\x.xlsx";
var fileName = "example.xlsx";
var file = new FileDto(fileName, MimeTypeNames.ApplicationVndOpenxmlformatsOfficedocumentSpreadsheetmlSheet);
var filePath = Path.Combine(_appFolders.TempFileDownloadFolder, file.FileToken);
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
File.Copy(fileSource, Path.Combine(filePath, fileName));
return file;
}
}
This service works fine. When I test it on swager, it give out a FileDto:
"result": {
"fileName": "example.xlsx",
"fileType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"fileToken": "9a864412394442758089b4256029cd48"
}
I confirmed It copies the file on C:\aspnet-core\src\SNet.Web.Host\wwwroot\Temp\Downloads\9a864412394442758089b4256029cd48\example.xlsx
However, when I try to download the file by url: <a class="postlink" href="http://localhost:22742/File/DownloadTempFile?fileType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&fileToken=9a864412394442758089b4256029cd48&fileName=example.xlsx">http://localhost:22742/File/DownloadTem ... ample.xlsx</a>,I get exception on code:
throw new UserFriendlyException(L("RequestedFileDoesNotExists"));
What did I miss? Does the "_appFolders.TempFileDownloadFolder" in FileController point to a different location?
Thanks,
Sorry, still don't understand. My need is simple: I have a file "x.jpg" stored in server folder "C:/Files/". How do I code something to let user download it to client side? Can you show me an code example?
I also need to deal other use-cases about files soon, such as embedding a file (e.g. a picture) into json object, and uploading files into server. I wonder if AspNetZero have built-in functions or easy-to-follow instructions about file handling. If not, it is understandable. I may stop digging it in AspNetZero and try other alternatives for handling files. Can you clarify this?
Thank you!
@drenton Thank you for your clue.
I found the class of "FileDownloadService" and "FileController", and studied some examples (e.g. getUsersToExcel). It seems to convert a file into a FileDto and return this FileDto as JSON format {"fileName", "fileType", "fileToken"}, but I still couldn't understand how to convert a file content to "fileToken" in server side and how to convert back the FileDto to original file and save it on client side.
Is there any instruction/tutorial/example about it? Let's say, I have a file example.jpg stored in folder App_Data. How do I build a service on both of Core and Angular sides?
Thanks,
The above aaron's code works fine in my methods. Now I have multiple methods (createObj, updateObj, GetObj, and so on) which need the same username, so I moved the same code into the service class constructor. The "GetCurrentUser().UserName;" always throw out exception: Invalid object name 'AbpUsers'.
What do I miss?
Thanks,
I found it. In .angular-cli.json, style: [ "../node_modules/primeng/resources/themes/bootstrap/theme.css",...].
primeNg has other themes in ../node_modules/primeng/resources/themes/ folder, e.g. "../node_modules/primeng/resources/themes/start/theme.css".
I followed that and fixed. Thank you very much!!
I like to try. It is just saying "Don't load en-US locale for angular". I wonder how to do it by my own, on which files?
Shall I expect the 5.0.5 released soon?
Thanks,
I tried that already and again just now, but no help. The console content shows same error. I also tried it on an old IE 8 which never opened this url, and got javascript error message as below:
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.5; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; MS STORE DMC2.6.3411.2) Timestamp: Wed, 6 Dec 2017 14:56:33 UTC
Message: Expected identifier Line: 1 Char: 29180 Code: 0 URI: <a class="postlink" href="http://dev1.bcnyintl.com/inline.b5e15fee162de528ff52.bundle.js">http://dev1.bcnyintl.com/inline.b5e15fe ... .bundle.js</a>
Message: Expected identifier, string or number Line: 1 Char: 2833 Code: 0 URI: <a class="postlink" href="http://dev1.bcnyintl.com/polyfills.510b5920d0b12586d917.bundle.js">http://dev1.bcnyintl.com/polyfills.510b ... .bundle.js</a>
Message: Expected identifier, string or number Line: 2 Char: 8672 Code: 0 URI: <a class="postlink" href="http://dev1.bcnyintl.com/scripts.e734b96cbef250b4cf8a.bundle.js">http://dev1.bcnyintl.com/scripts.e734b9 ... .bundle.js</a>
Message: Expected identifier Line: 1 Char: 57378 Code: 0 URI: <a class="postlink" href="http://dev1.bcnyintl.com/main.649a1ba7e1ef35a5c419.bundle.js">http://dev1.bcnyintl.com/main.649a1ba7e ... .bundle.js</a>