iTextSharp add image from route - itext

I'm trying to retrieve an image in iTextSharp. The image is retrieved by calling: local.myapp.com/File/FileDownload/102737. The images are in a database and the FileDownload actionresult method gets the image according to the id provided.
I'm trying to call above url from with a C# class (not controller) like this
iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath($"{imgName}"));
However I'm not able to make it work. I get wrong path something beginning with: c:\windows\system32\inetsrv\

As discussed in comments to the question, the image
has to be retrieved through http.
Thus, instead of using the Image.GetInstance overload with a string argument (which assumes the argument to be a file name in the local file system), use the overload with an Uri argument:
/// <summary>
/// Gets an instance of an Image.
/// </summary>
/// <param name="url">an URL</param>
/// <returns>an object of type Gif, Jpeg or Png</returns>
public static Image GetInstance(Uri url)

Related

How to get My SharedPreference stored string in non Class file in dart?

I have constants.dart file and it contain all constant file including base url of my REST API. I need to change my REST API URL based on selected city which is already stored in shared preferences. I've attached my Code below.
Here is the Code

Struts 2 post back default

In the Struts documentation, it says:
Another common workflow stategy is to first render a page using an alternate method, like input and then have it submit back to the default execute method.
https://struts.apache.org/core-developers/action-configuration.html#post-back-default
How to do it using annotation only? It seems that only the execute() method is called.
In the documentation it's said to render a page can be used an alternate method like input. This means that when you submit a form on the page it can return back with the input result. Usually it happens automatically during validation process if the validation fails or it hasErrors. Then you can submit the form back to the default action's execute method. You don't need to specify a method in the action configuration. Also if you didn't specify the action attribute in the form tag then the same action will execute which was used to render a page.
Configuring actions you can use the same page for success result when rendering a page using GET method and input when POST method is requested.
To use annotations to configure actions mapping you can use a Convention Plugin.
Also note, to map a class method to the action you should put #Action annotation directly on this method rather than on the class.
More detailed explanation and documentation you can find here.
#Namespace("/")
public class ProductAction extends ActionSupport {
public String execute() {
return SUCCESS;
}
#Action(value="product",
results=#Result(location="/product-list.jsp")
)
public String search() {
return SUCCESS;
}
}
Notice, that the method execute is not mapped, so it will not execute. If you need that method execute you should create mapping to it. For this purpose you could place annotation on class or on method execute.

play framework - redirecting with querystring

I am Using the play Framework 2.7.x
I have a Formular on my controller.list() with a view, let's call it "index". After you click "send" it open's controller.add() where it dos some stuff and then redirects back to controller.list(). If there was an error in the formular (a requiered field was empty) I need the queryString, which was send to controller.add() also redirected to controller.list()
The problem ist that if I do stuff like just passing the request, i get an error that it's not possible to add arguments.
public Result list(Http.Request request)
{
// .... stuff with foo, while foo is an Form<foo> Object
// ... foo.bindFromRequest(request)
ok(views.html.index.render(foo))
}
public Result add(Http.Request request)
{
// not allowed to add request as an argument. only empty is allowed.
return Results.redirect(controllers.routes.Controller.list(request));
}
I would like to just redirect the Form object, so I can handle the error in the controller.list() and not have to generate an extra view for the controller.add(). If I do everything inside controller.list() there is no problem with this code, but I like to use the controller.add() method instead.
Is the an option? except passing every querystring key and value by hand.
After I searched yesterday the half the day, I found something interessting today.
you are not allowed to use a default parameter with =. You have to use an optional default parameter with ?= inside the routes!!!!!
you can implement QueryStringBindable so it's a bit easier to bind the query String. But you still have to bind them "by hand".

Set a response page with instance and parameter

I'm stuck with a redirection in Wicket (1.5) and the different versiont of the setResponsePage.
I mount a page with parameters, but I cant use the version with the class sinc I want to use a specific constructor to create this page in order to pass some arguments. When I do that, the generated url does not display the parameters.
Here is the code :
// WicketApplication
mount(new MountedMapper("create/${param}/full", MyPage.class));
// In a page
PageParameters parameters = new PageParameters();
parameters.add("param", "value");
// URL OK : create/value/full
setResponsePage(MyPage.class, parameters);
// URL KO : create//full
setResponsePage(new MyPage(parameters, arguments...));
Is there any way to set a custom response page with parameters and an instance of a page ? A way to do something like setResponsePage(new MyPage(parameters, arguments...), parameters);
Make these two changes:
Use overloaded constructors, one taking only PageParameters for when you need to pass parameters, and the other taking only an IModel for when you need to pass a model.
Change the $ to a # to make your param place holder optional and solve your URL issue:
mount(new MountedMapper("create/#{param}/full", MyPage.class));

Mounted mapper with named parameters also receives requests for image

In my application I mount following URL:
this.mountPage("/details/${site}", MerchantDetailPage.class);
So a request to for instance ../details/anything will create an instance of MerchantDetailPage with pageparameter: site=anything.
The constructor of MerchantDetailPage:
public MerchantDetail(final PageParameters parameters) {
super();
org.apache.wicket.util.string.StringValue storeParameter = parameters.get("site");
if (!storeParameter.isEmpty()) {
this.store = this.service.getStoreByQBonSiteWithCategoriesDescriptionsRegionAndAddress(storeParameter.toString());
}
if (store == null) {
throw new RestartResponseAtInterceptPageException(Application.get().getHomePage());
}
// Build the page
this.createPage(this.store, null);
}
This seemed to work fine until I noticed that the constructor was called 4 times.
After some digging I found that the constructor was called once with parameter site=anything but then another 3 times for 3 images that are on the page; e.g.:
<img wicket:id="store_no_image" src="./images/shop_no_logo_big.png" alt="logo" />
So, for this resource Wicket is also calling this page but with parameter: site=images.
As a consequence, the store is null so the request for the image is redirected to the homepage => the image is not found.
Why is this happening? Why is wicket trying to treat a resource request through a page mount?
Some side comments:
MerchantDetailPage has also another constructor which is called directly from the code and accepts the store id as a parameter. In this case the problem does not occur.
if I use an absolute URL for the image it does work (does not enter into MerchantDetailPage for the image request)
Well... your page resides at
/detail/anything
which is correctly mapped to your merchant detail page...
Your images reside at
/detail/images/shop_no_logo_big.png
and similar, which is correctly mapped to your merchant detail page...
The mount path doesn't know and doesn't care if it's a page request or a resource request. For all it is worth it could be the case that you're using the mount path to create the resource dynamically...
So the solution is to move your images to a location that doesn't match yout mount-path.