Posting SVG string to Controller works locally but not on server - asp.net-mvc

I have a form that I'm posting that contains 2 numeric values and a long string containing an SVG (generated using Fabricjs). The controller uploads the SVG and outputs a PDF containing it. The reason I'm using a standard form post instead of Ajax POST is because I understand that ajax cannot return a file download.
I have maxJsonLength, aspnet:MaxJsonDeserializerMembers, and maxRequestLength all set to values much higher than what's involved here (10k of text), and I don't think the json-related ones come into play here anyway since it's a standard form post.
This works perfectly when I run it locally in Visual Studio. Once it's published to the server, it fails. I attached to the process with remote debug, and it's not even hitting the controller. I have a similar controller action that also accepts the same SVG string, and that's not working either.
In the network console, the action returns a 200. When I look at the headers, I can't see any difference between what's posting to the server vs. what I see locally. Elmah is not capturing any error, so I can't even figure out where to go at this point to find out what's wrong.
Here's the form. I added the enctype at the suggestion of another SO post, but that doesn't make a difference.
<form action="/Product/ExportSVG" enctype="application/x-www-form-urlencoded; charset=UTF-8" id="svgForm" method="post">
<input type="hidden" id="svgProjNo" name="svgProjNo" value="1111">
<input type="hidden" id="svgLineID" name="svgLineID" value="">
<input type="hidden" id="svgData" name="svgData" value="">
</form>
The script that posts the form:
var svgtext = canvas.toSVG(null, reviver);
$("#svgLineID").val($("#LineItem_ParentID").val());
$("#svgData").val(svgtext);
$('#svgForm').trigger('submit');
The controller:
[HttpPost]
public ActionResult ExportSVG(decimal svgProjNo, decimal svgLineID, string svgData)
{
...
}

You may need to encode SVG data in base 64 format.

Related

Sending long text from web form to server

I try to move my text analyzer from console to web form.
I have simple form like this:
<form action="/">
<textarea name="str"></textarea>
<input type="submit">
</form>
Generally I could have very long texts for analysis inside of textarea. When I submit the form I get the following from thin:
Invalid request: Header longer than allowed
So the question is what is the proper approach to send long texts to server? Uploading files or filling links to url are not the option unfortunately.
By default the method of a form is GET, which has a limit on the number of characters allowed. (The limit depends on server and client, see for instance this answer, which specifies that usually it is 8KB).
You should use instead a method POST, which has much larger limit, around 2GB.
<form action="/" method="POST">
<textarea name="str"></textarea>
<input type="submit">
</form>

Input form in ASP.NET MVC to PDF and then sent to an email

I currently have a input form where the user can register to a website I have
created this form In MVC but NOT using razor but using the ASPX view engine
and for this reason I am unable to find examples which help. However, once the
user has completed the form I want the results from the form to be converted to
a pdf file and then that pdf file to be sent to a specific email address.
The input form is all done in HTML and CSS so I have only used InputBoxes such as:
<input id="Name" type="text">
After a few more fields I have a submit button like:
<input id="Submit" type="submit">
I didn't include the full HTML code because its just repetition but if you would like
the full code let me know
I know doing this is quite extensive I probably first need to start with a
PDF conversion tool but is there any other ways of doing this can someone direct me in the right direction?

Clarifying POST-ing to a site

Say I were to post to a site using www.mysite.com?user=myuser does that simulate the submit button that is associated with that form? If so, what happens if there are a number of submit buttons in the form?
Also, if that button's html is like so <input name="button" class="button" type="button" value="Save" onclick="javascript: submit()" disabled> with the "disabled" attribute, does that mean I can't POST www.mysite.com?user=myuser/won't work?
<form name="Form" method="post" action="/thisAction.do">
<input type="text" name="inquiryNo" maxlength="11" value="" onkeyup="javascript: checkNo()">
<input name="buttonInquire" class="button" value="Inquire" onclick="javascript: submitInquire()" type="button">
<!--Then comes a number of other inputs-->
<input.../>
<input.../>
<input.../>
<input name="modify" class="button" type="button" value="Save"
onclick="javascript: submitModify()" disabled>
</form>
This is some sample code as it's work stuff which I am not allowed to share. So when I use the inquire action a new account successfully loads up and the details are presented on the page. The modify action is meant to 'modify' those details but it just returns the same details displayed on the page. I get no sort of feedback from anything.
You can POST to a URL with a query string (the stuff after the ?), and since you say you're using urllib2 with a data argument, that's what happens. The server can then read both the POST data and the query string and do whatever it wants, though most of the time they're merged together or the query string is ignored entirely.
disabled only stops you from clicking the button in the browser (and even then, you can just un-disable it with a tool like Firebug). You can POST whatever you want to any URL you want; HTML can't stop you, though the server can still give you an error if it wants.
I think your problem is that "inquire" is the default action, and something's wrong with your POST. So no matter what you send, the server isn't recognizing it and is falling back to "inquire".
It looks like the form is intended to send modify=Save. Can you post the Python code you're actually running?
No, what you actually typed in is a GET method.
There are 2 ways of submitting data: POST AND GET.
Post is by submitting data by using a form in a webpage that posts to another on the background, while GET is setting the data in the url itself like user=myuser.
Most of the times using a GET method (url query string) will not work if the web programmer actually is checking for a POST method. The same happens if the programmer is waiting for a GET request and you actually POST it.
However there is a php var called REQUEST which will work with GET and POST.
I'm no professional in PHP but because you had no answers at the moment I tried my best to explain it. Hopefully some expert will come along and explain it properly.
You edited your question while I was replying so you need someone to answer you on your second question.

Refactoring HTML Markup from POST to GET

I have the following mark up in an ASP.NET MVC view (this is a Twitter Bootstrap search box):
<form action="#Url.Action("Results", "Search")" method="post">
<input type="text" class="search-query" id="SearchTerm" name="SearchTerm" />
</form>
This code works as expected, but using a post here is causing problems.
How can I change this markup to pass the search query as a URL argument instead? I'm not really sure how to even approach this short of keeping the existing markup and then redirecting from the controller. I'm thinking there must be a more efficient way than that.
You should be able to change method="post" to method="get" and get the desired result. The form, with a get method setting, pushes the fields in the form to the querystring by its default behavior.
As a workaround, if the default behavior doesn't suit you, you could catch the submit event of the form, and do:
window.location = form.action + "?SearchTerm=" + document.getElementById("SearchTerm").value
Something like that, where form is a reference to the form element. You can build the link and redirect using javascript, which is a get request.

Insert cakephp POST params into URL

I have this form below which contains two checkboxes to sort some products:
<form id="FiltreExtraForm" action="" method="post" name="FiltreExtraForm">
<input id="ProductsDeliveryPrice" type="checkbox" value="1" name="data[Products][delivery_price]"/>
<input id="ProductsPicture" type="checkbox" value="1" name="data[Products][picture]"/>
</form>
After POST I do the filtering but I also want to add received parameters to URL E.g: /products/index/delivery_price:1/picture:0 . Is this possible. How can I do that?
Note: I don't want to use GET to send form info.
Sounds like you are looking to do a Post/Redirect/Get.
Here are two examples of doing this in CakePHP:
Searching on surname
Searching on multiple fields
The two main advantages of redirecting a POST to a GET request are:
Users don't get the "Do you want to resubmit?" dialog if they refresh
The resulting page/query can be bookmarked
In the action to which you post, you could simply prepare the GET url and then redirect to this url. The action for that url then does the filtering.
If I understand you correctly (and I'm not sure that I do), you can pass additional variables on the query string of the form's action quite easily. Conventionally, that might look like this:
<form id="FiltreExtraForm" action="/products/index?delivery_price=1&picture=0" method="post" name="FiltreExtraForm">
Using Cake, you should be able to do the same without the traditional query string if you'd rather (though the traditional method above will also work):
<form id="FiltreExtraForm" action="/products/index/delivery_price:1/picture:0" method="post" name="FiltreExtraForm">
I would recommend looking at the form helper or at least constructing the action URI using helpers, but this should get you what you're after.

Resources