I'm still a beginner in JavaScript and ajax. I have this problem in my code where I try to upload an image from an ajaxfileupload but the error for JavaScript will always popup, saying that it has an internal server error. What possible mistakes I made in my code?
Here is the aspx code:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CreateBrands.aspx.cs" Inherits="Pages_CreateBrands" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="asp"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Music Store</title>
<script src="../Javascript/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
function uploadComplete(sender, args) {
var txt = document.getElementById("validatePicture1");//Your
hiddenfield id
txt.value = "1";
$.ajax({
type: "POST",
url: "CreateBrands.aspx/GetPath1",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
OnSuccess1(result.d);
},
error: function (xhr, status, error) {
OnFailure1(xhr,status,error);
}
});
}
function OnSuccess1(result) {
var pp = document.getElementById("PicturePath1");
pp.value = result;
}
function OnFailure1(xhr,status,error) {
alert("Request: " + xhr + "\n\nStatus: " + status + "\n\nError: " +
error);
}
</script>
<script type="text/javascript">
function uploadComplete2(sender, args) {
var txt = document.getElementById("validatePicture2");//Your
hiddenfield id
txt.value = "1";
$.ajax({
type: "POST",
url: "CreateBrands.aspx/GetPath2",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
OnSuccess1(result.d);
},
error: function (xhr, status, error) {
OnFailure1(xhr,status,error);
}
});
}
function OnSuccess1(result2) {
var pp = document.getElementById("PicturePath2");
pp.value = result;
}
function OnFailure1(xhr,status,error) {
alert("Request: " + xhr + "\n\nStatus: " + status + "\n\nError: " +
error);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="wrapper">
<div id="content_area">
<h3> </h3>
<h3 class="headingTitle">Create New Brand(Step 1 of 2):</h3>
<table cellspacing="15" class="brandsTable">
<br/>
<h3 class="headingTitle">Create New Item(Step 2 of 2):</h3>
<table cellspacing="15" class="brandsTable">
<tr>
<td style="width: 160px; height: 37px;">
<strong>Item Type:</strong></td>
<td style="height: 37px">
<asp:RadioButton ID="itemType1" runat="server"
Text="Guitar" AutoPostBack="False" GroupName="itemType"/>
<asp:RadioButton ID="itemType2" runat="server"
Text="Bass" AutoPostBack="False" GroupName="itemType"/>
</td>
</tr>
<tr>
<td style="width: 160px; height: 37px;">
<strong>Item Image1:</strong></td>
<td style="height: 37px">
<br />
<asp:AjaxFileUpload ID="itemFileUpload1"
runat="server" OnUploadComplete="itemUploadImage1_Click"
OnClientUploadComplete="uploadComplete" MaximumNumberOfFiles="1"/>
<asp:HiddenField ID="validatePicture1" Value=""
runat="server" />
</td>
</tr>
<tr>
<td style="width: 160px; height: 37px;">
<strong>Item Image2:</strong></td>
<td style="height: 37px">
<br />
<asp:AjaxFileUpload ID="itemFileUpload2"
runat="server" OnUploadComplete="itemUploadImage2_Click"
OnClientUploadComplete="uploadComplete2" MaximumNumberOfFiles="1"/>
<asp:HiddenField ID="validatePicture2" Value=""
runat="server" />
</td>
</tr>
</table>
<asp:Label ID="lblResult2" runat="server" Text="">
</asp:Label>
<br />
<asp:Button ID="Button1" runat="server"
CssClass="submitButton" Text="Save Item" OnClick="Button1_Click"/>
</div>
</div>
</form>
</body>
</html>
And here is the code-behind. By the way, it also has an object reference null in the method GetPath1 with this line of code: string retval = Session["PicturePath1"].ToString();:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
public partial class Pages_CreateBrands : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void ClearTextFields2()
{
itemBrand.Text = "";
itemModel.Text = "";
itemPrice.Text = "";
itemDescription.Text = "";
itemNeckType.Text = "";
itemBody.Text = "";
itemFretboard.Text = "";
itemFret.Text = "";
itemNeckPickup.Text = "";
itemBridgePickup.Text = "";
itemBridge.Text = "";
itemHardwareColor.Text = "";
}
[System.Web.Services.WebMethod]
public string GetPath1()
{
System.Web.SessionState.HttpSessionState Session =
System.Web.HttpContext.Current.Session;
string retval = Session["PicturePath1"].ToString();
Session["PicturePath1"] = null;
return retval;
}
[System.Web.Services.WebMethod]
public string GetPath2()
{
System.Web.SessionState.HttpSessionState Session =
System.Web.HttpContext.Current.Session;
string retval = Session["PicturePath2"].ToString();
Session["PicturePath2"] = null;
return retval;
}
protected void itemUploadImage1_Click(object sender, AjaxFileUploadEventArgs
e)
{
if (itemType1.Checked)
{
string filename = e.FileName;
Session["PicturePath1"] = filename;
itemFileUpload1.SaveAs(Server.MapPath("~/Images/Brands/String
Instrument Items/Guitar/") + filename);
}
else if(itemType2.Checked)
{
string filename = e.FileName;
Session["PicturePath1"] = filename;
itemFileUpload1.SaveAs(Server.MapPath("~/Images/Brands/String
Instrument Items/Bass/") + filename);
}
}
protected void itemUploadImage2_Click(object sender, AjaxFileUploadEventArgs
e)
{
if (itemType1.Checked)
{
string filename = e.FileName;
Session["PicturePath2"] = filename;
itemFileUpload2.SaveAs(Server.MapPath("~/Images/Brands/String
Instrument Items/Guitar/") + filename);
}
else if (itemType2.Checked)
{
string filename = e.FileName;
Session["PicturePath2"] = filename;
itemFileUpload2.SaveAs(Server.MapPath("~/Images/Brands/String
Instrument Items/Bass/") + filename);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (itemType1.Checked)
{
int item_type =
ConnectionClassBrands.GetIdByType(itemType1.Text);
int item_brandId =
ConnectionClassBrands.GetIdByBrand(itemBrand.Text);
string item_model = itemModel.Text;
double item_price = Convert.ToDouble(itemPrice.Text);
string item_image1 = Session["PicturePath1"].ToString();
string item_image2 = Session["PicturePath2"].ToString();
string item_description = itemDescription.Text;
string item_necktype = itemNeckType.Text;
string item_body = itemBody.Text;
string item_fretboard = itemFretboard.Text;
string item_fret = itemFret.Text;
string item_bridge = itemBridge.Text;
string item_neckpickup = itemNeckPickup.Text;
string item_bridgepickup = itemBridgePickup.Text;
string item_hardwarecolor = itemHardwareColor.Text;
ConnectionClassGuitarItems.AddStringInstrumentItems(item_type,
item_brandId, item_model, item_price, item_image1, item_image2,
item_description, item_necktype, item_body, item_fretboard,
item_fret, item_bridge, item_neckpickup,
item_bridgepickup, item_hardwarecolor);
lblResult2.Text = "Upload successful!" + item_image1 + " and " +
item_image2;
}
else if (itemType2.Checked)
{
try
{
int item_type =
ConnectionClassBrands.GetIdByType(itemType2.Text);
int item_brandId =
ConnectionClassBrands.GetIdByBrand(itemBrand.Text);
string item_model = itemModel.Text;
double item_price = Convert.ToDouble(itemPrice.Text);
string item_image1 = Session["PicturePath1"].ToString();
string item_image2 = Session["PicturePath2"].ToString();
string item_description = itemDescription.Text;
string item_necktype = itemNeckType.Text;
string item_body = itemBody.Text;
string item_fretboard = itemFretboard.Text;
string item_fret = itemFret.Text;
string item_bridge = itemBridge.Text;
string item_neckpickup = itemNeckPickup.Text;
string item_bridgepickup = itemBridgePickup.Text;
string item_hardwarecolor = itemHardwareColor.Text;
ConnectionClassGuitarItems.AddStringInstrumentItems(item_type,
item_brandId, item_model, item_price, item_image1, item_image2,
item_description, item_necktype, item_body,
item_fretboard, item_fret, item_bridge, item_neckpickup,
item_bridgepickup, item_hardwarecolor);
lblResult2.Text = "Upload successful!";
ClearTextFields2();
}
catch (Exception ex)
{
lblResult2.Text = ex.Message;
}
}
else
{
Response.Redirect("~/Pages/OverviewGuitarData.aspx");
}
}
}