i Use This Code For File Upload But NotWorking
Default.aspx Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <h3>FileUpload.SaveAs Method Example</h3> <form id="Form1" runat="server"> <h4>Select a file to upload:</h4> <asp:FileUpload id="FileUpload1" runat="server"> </asp:FileUpload> <br /><br /> <asp:Button id="UploadButton" Text="Upload file" OnClick="UploadButton_Click" runat="server"> </asp:Button> <hr /> <asp:Label id="UploadStatusLabel" runat="server"> </asp:Label> </form> </body> </html>
And This IS a Default.aspx.cs Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void UploadButton_Click(object sender, EventArgs e) { // Before attempting to save the file, verify // that the FileUpload control contains a file. if (FileUpload1.HasFile) // Call a helper method routine to save the file. SaveFile(FileUpload1.PostedFile); else // Notify the user that a file was not uploaded. UploadStatusLabel.Text = "You did not specify a file to upload."; } void SaveFile(HttpPostedFile file) { // Specify the path to save the uploaded file to. string savePath = "~/Uploads/"; // Get the name of the file to upload. string fileName = FileUpload1.FileName; // Create the path and file name to check for duplicates. string pathToCheck = savePath + fileName; // Create a temporary file name to use for checking duplicates. string tempfileName = ""; // Check to see if a file already exists with the // same name as the file to upload. if (System.IO.File.Exists(pathToCheck)) { int counter = 2; while (System.IO.File.Exists(pathToCheck)) { // if a file with this name already exists, // prefix the filename with a number. tempfileName = counter.ToString() + fileName; pathToCheck = savePath + tempfileName; counter++; } fileName = tempfileName; // Notify the user that the file name was changed. UploadStatusLabel.Text = "A file with the same name already exists." + "<br />Your file was saved as " + fileName; } else { // Notify the user that the file was saved successfully. UploadStatusLabel.Text = "Your file was uploaded successfully."; } // Append the name of the file to upload to the path. savePath += fileName; // Call the SaveAs method to save the uploaded // file to the specified directory. FileUpload1.SaveAs(savePath); } }
And This Error Shows Me When I Run It
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
And What am I Doing to fix This? Thanks
use this simple code hope will helpfull to you for uploading an image
string _fname,_fpath;
if (FileUpload1.HasFile)
{
_fname = FileUpload1.FileName.ToString();
_fpath="~/upload/"+_fname.ToString();
FileUpload1.SaveAs(Server.MapPath("~/upload/") + _fname);
SqlCommand cmd = new SqlCommand("insert into tablename(Name,Image) values('" + TextBox1.Text + "','" + _fpath + "')", con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Data inserted successfully";
con.Close();
TextBox1.Text = "";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.