Line 23: string imgPath = "ImageStorage/" + imgName;
Line 24: //then save it to the Folder
Line 25: FileUpload1.SaveAs(Server.MapPath(imgPath));
Line 26:
Line 27: //get the size in bytes that
Can you post your source code for both the aspx file and the aspx.cs file so i can take a quick look, I think i might know what it is just need to see both sides to make sure.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void StartUpLoad()
{
//get the file name of the posted image
string imgName = FileUpload1.FileName.ToString();
//sets the image path
string imgPath = "ImageStorage/" + imgName;
//then save it to the Folder
FileUpload1.SaveAs(Server.MapPath(imgPath));
//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;
//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file
//Call the method to execute Insertion of data to the Database
ExecuteInsert(imgName, imgSize, imgPath);
Response.Write("Save Successfully!");
}
}
}
private string GetConnectionString()
{
//sets the connection string from your web config file. "DBConnection" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString;
}
private void ExecuteInsert(string name, int size, string path)
{
richkyrs
Member
436 Points
500 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 07:30 PM|LINK
hi justin it gives me this code on line 25
Line 23: string imgPath = "ImageStorage/" + imgName;
Line 24: //then save it to the Folder
Line 25: FileUpload1.SaveAs(Server.MapPath(imgPath));
Line 26:
Line 27: //get the size in bytes that
Justin Dwyer
Participant
1107 Points
195 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 07:35 PM|LINK
Can you post your source code for both the aspx file and the aspx.cs file so i can take a quick look, I think i might know what it is just need to see both sides to make sure.
richkyrs
Member
436 Points
500 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 07:42 PM|LINK
this is the default2.aspx source
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
this is the default2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void StartUpLoad()
{
//get the file name of the posted image
string imgName = FileUpload1.FileName.ToString();
//sets the image path
string imgPath = "ImageStorage/" + imgName;
//then save it to the Folder
FileUpload1.SaveAs(Server.MapPath(imgPath));
//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;
//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file
//Call the method to execute Insertion of data to the Database
ExecuteInsert(imgName, imgSize, imgPath);
Response.Write("Save Successfully!");
}
}
}
private string GetConnectionString()
{
//sets the connection string from your web config file. "DBConnection" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString;
}
private void ExecuteInsert(string name, int size, string path)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO ImageInfo (ImageName, ImageSize, ImagePath) VALUES "
+ " (@ImgName,@ImgSize,@ImgPath)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@ImgName", SqlDbType.NVarChar, 50);
param[1] = new SqlParameter("@ImgSize", SqlDbType.BigInt, 9999);
param[2] = new SqlParameter("@ImgPath", SqlDbType.VarChar, 50);
param[0].Value = name;
param[1].Value = size;
param[2].Value = path;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
StartUpLoad();
}
}
Justin Dwyer
Participant
1107 Points
195 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 07:49 PM|LINK
Have you created the ImageStorage folder?
richkyrs
Member
436 Points
500 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 07:57 PM|LINK
i did but i created it like this Image_Storage rather than ImageStorage your spot on thankyou now fixed
see it says saved succefully , but doesnt show on the profile of any user?