I am experiencing a very strange behavior. I am doing a simple ASP .NET application that will upload a file to the server.
I have a simple form that does this:
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="files" class="col-md-3 col-sm-4 control-label">Bestand *</label>
<div class="col-md-9 col-sm-8" ">
<input name="file" type="file" id="file" required />
<span >#ViewBag.TheMessage</span>
</div>
</div>
<div class="form-group">
<div class="col-md-9 col-sm-8 col-md-push-3 col-sm-push-4">
<input type="submit" class="btn btn-primary" value="Versturen" />
</div>
</div>
</form>
The strange thing happens with the POST, 9 of 10 times this POST goes to test.com instead of localhost. I sniffed it with firebug to discover this. The firebug looks like this:
The Controller.cs looks like this, and it's working fine when post reached:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
ViewBag.TheMessage = "Thanks, an e-mail will be sent shortly";
if (file.ContentLength > 0)
{
ViewBag.Message = "Your application description page.";
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return View();
}
Another strange thing is that if I just use an empty format cshtml file only with:
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit"/>
POST woks fine every time.
Thanks.
Check to see if you have any Javascript that is changing your forms action attribute. I would start by looking at onload, onsubmit, or on your submit buttons onclick method. If you're using jquery be sure to check your $(document).ready () function. This really could be anywhere in your js though so make sure you look everywhere.
Another culprit could be that you have a base tag in your html that looks something like <base href="http:www.test.com /> which will change all of your relative urls to go that base location
Related
Sorry I am quite new in ASP.NET MVC and I have this piece of code in the Home view folder (file Index.cshtml):
#{
ViewBag.Title = "Home Page";
}
<div class="row">
<div class="col-md-4">
<h2>Select file</h2>
<p>
<input id="File1" type="file" />
</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more ยป</a></p>
</div>
<div class="col-md-4">
<h2>Upload to SQL Server</h2>
<input type="button" title="Upload to SQL Server" value="Upload to SQL Server" onclick="location.href='#Url.Action("Upload2SS", "SystemLogs")'" />
</div>
</div>
When I click the "Upload to SQL Server" button, I simply want to pass the File1 value to the controller action:
public RedirectToRouteResult Upload2SS(FormCollection form)
{
string filePath = form["File1"].ToString();
var data = GetDataTabletFromCSVFile(filePath);
return RedirectToAction("Index");
}
However, I keep on getting a System.NullReferenceException for the filePath variable; can anyone tell me what I am missing please?
Your code missing a lot of things. First of all you need to use a form tag in which you will have input file and input button.
This form will help you to submit bunch of information to the server. Similarly you need to set the name attribute, From your code you are missing name attribute in your input file.
You can search Stackoverflow and find any answer. For submitting file, read this answer https://stackoverflow.com/a/28380690/713789
I have a simple form for payment:
<form method="POST" action="https://www.liqpay.ua/api/3/checkout" accept-charset="utf-8">
<input name="data" value="{{data}}" />
<input name="signature" value="{{signature}}" />
<input type="image" src="//static.liqpay.ua/buttons/p1ru.radius.png" />
</form>
The problem is that I can't press the button for payment, it's just don't work
I can assure you that the form is working I've checked this on C# with some changes:
<div class="container">
<form method="POST" action="https://www.liqpay.ua/api/3/checkout" accept-charset="utf-8">
<input type="hidden" name="data" value="#ViewData["PaymentData"]" />
<input type="hidden" name="signature" value="#ViewData["Signature"]" />
<input type="image" src="//static.liqpay.ua/buttons/p1ru.radius.png" />
</form>
</div>
And all the parametress a passed correctly. It seems like Angular dont let me go to the link or sth like that. Help pls.
UPDATE
Ok I changed the form like this:
<form [formGroup]="payForm" (ngSubmit)="LiqPay()" #formDir="ngForm" novalidate>
<input formControlName="data" name="data" value="{{data}}" />
<input formControlName="signature" name="signature" value="{{signature}}" />
<button type="submit" class="btn btn-default" ng-href="https://www.liqpay.ua/api/3/checkout">go</button>
</form>
with LiqPay method:
LiqPay(){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.payForm.value.data = this.data;
this.payForm.value.signature = this.signature;
this._http.post('https://www.liqpay.ua/api/3/checkout',
this.payForm,{headers}).map((response: Response) => response.json());
}
Now I guess the request is sent but it doesn't redirect on the desired link (https://www.liqpay.ua/api/3/checkout). So how can I change the form to redirect to this link with post data?
In the LiqPay method you are trying to send a post request to https://www.liqpay.ua/api/3/checkout. And in the button you have a ng-href attribute (trying to browser go to that url). I think that is not correct. You are sendind an asynchronous requets, and simultaneously sendind your user to that url.
I can understand, you are sending a form to a payment checkout? ok, you can use action in the form, exactly like the C# example.
I have tested your initial code
<form method="POST" action="https://www.liqpay.ua/api/3/checkout" accept-charset="utf-8">
<input name="data" value="{{data}}" />
<input name="signature" value="{{signature}}" />
<input type="image" src="//static.liqpay.ua/buttons/p1ru.radius.png" />
</form>
And it works...
You can view it here:
https://jsfiddle.net/L1ruajzn/
<form action="#Url.Action("PokemonSteps", "PokemonView", new { id = Model.Id, steps = steps })">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit">
</form>
This form is a number box from 1 to the amount of steps the user has.
When they submit the number in the box, I want to pass it to #Url.Action as the steps in the steps = steps part.
How do I go about that (sorry for the really stupid question)
<form action="#Url.Action("PokemonSteps", "PokemonView")" method="post">
<input type="hidden" name="id" value="#Model.Id">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit" value="Submit">
</form>
Please add Attribute method="post" and share your controller method signature also...
Try doing something like this:
#using (Html.BeginForm("PokemonSteps", "PokemonView", FormMethod.Post)){
#Html.TextBoxFor(m=> m.userSteps)
<input type="submit" value="Submit" />
}
Hope this Helps!!
I am fairly new to C# and asp.net and am having trouble showing something submitted in a form. The form element is a tag. The drop-down info is pulled from a data base and displays correctly. It's just getting it to post to another page after the form is submitted is where I am having the problem. Any help would be appreciated.
Contact.aspx:
<form action="Default.aspx" method="post" data-transition="pop">
<div data-role="fieldcontain">
<label for="topEmails">Business Entity ID:</label>
<select name="topEmails" id="topEmails" data-native-menu="false"
runat="server">
</select>
</div>
<input type="submit" data-iconpos="right" data-inline="true"
data-icon="plus" name="sendMessage" id="sendMessage" value="Send Info">
</form>
Contact.aspx.cs:
AdventureWorks2012DataContext db = new AdventureWorks2012DataContext();
var emails = (from b in db.EmailAddresses
select new { b.EmailAddressID, b.BusinessEntityID }).Take(20);
topEmails.DataTextField = "BusinessEntityID";
topEmails.DataValueField = "BusinessEntityID";
topEmails.DataSource = emails;
topEmails.DataBind();
Default.aspx.cs:
FormSuccessBID.InnerHtml = "Business Entity ID: " + Request.Form["topEmails"] + "";
Any ideas why this wouldn't be working?
Update:
Contact.aspx:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2 style="text-align: center;">Contact Kyle</h2>
<form action="Default.aspx" method="post" data-transition="pop">
<div data-role="fieldcontain">
<label for="userFName">First Name:</label>
<input type="text" name="firstName" id="uFName">
</div>
<div data-role="fieldcontain">
<label for="userLName">Last Name :</label>
<input type="text" name="lastName" id="uLName">
</div>
<div data-role="fieldcontain">
<label for="productsCategories">Products:</label>
<select name="productCategories" id="productCategories" data-native-menu="false" runat="server"></select>
</div>
<div data-role="fieldcontain">
<label for="topEmails">Business Entity ID:</label>
<select name="topEmails" id="topEmails" data-native-menu="false" runat="server"></select>
</div>
<input type="submit" data-iconpos="right" data-inline="true" data-icon="plus" name="sendMessage" id="sendMessage" value="Send Info">
</form>
</asp:Content>
You're missing "runat='server'" in your form definition.
In ASP.NET you're also allowed one form per page. Not sure from what you're posted if you're trying to put multiple forms on there, but if you are, it's not going to work.
I think all you need to do to accomplish what you want is have an ASP.NET button on the page, with the postbackURL set to the page you want to go to. (Of course, you need to do this in a server-side form)
I think you're mixing ASP.NET methods with other, different technologies. (From the look of it, you probably used classic ASP or PHP perhaps?)
If you can't use .NET for whatever reason, the ID/name of the field is not going to be what you're expecting. You need to inspect the form post and find its value - something along the lines of "clt00_somethingelse_topEmails". (Again, this is going to be a heck of a lot easier if you use the .NET way of doing things, but you may have a requirement not to)
I have a form
<form action="/" method="post">
<input type="file" name="myFile" />
<input type="submit" name="submit" value="submit" />
</form>
I also have some C# code
if (Request["submit"] == "submit") {
Response.Write(Request.Files.Count);
}
If a user chooses a file on their system and submits, What reasons could there be for me seeing a "0" instead of a "1" in the Request.Files.Count property?
Try adding the enctype attribute to your <form>
<form enctype="multipart/form-data" action="/" method="post">
If you are using any kind of ajax (i.e. update panel) then you have to make sure you're doing a full page post back when submitting not a partial page postback.