multifileupload in asp.net

Posted by Dn2010 under ASP.NET on 12/19/2010 | Points: 10 | Views : 1792 | Status : [Member] | Replies : 3
hi friends,
this code is multifileupload using jquery .
that code store file/images is image folder.
but i want to store file/images in database.
so please correct this code.

thanks.

protected void jQueryUploadFiles(object sender, EventArgs e)
{
FileUploadUsingJQuerySelectionMethod();
}

/// <summary>
/// file upload file using ASP.NET FileUpload control
/// </summary>
/// <param name="fileUpload"></param>
private void UploadFileUsingASPNET(FileUpload fileUpload)
{
try
{

// check if file has been selected
if (fileUpload.HasFile)
{
string path = Server.MapPath("~/MultipFileUpload/");
string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);

// check for the valid file extension
string fileExtension = Path.GetExtension(fileName).ToLower();
if (fileExtension.Equals(".gif") || fileExtension.Equals(".jpg"))
{
// now save the file to the disk
fileUpload.SaveAs(path + fileName);

lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully ! <br />";
}
else
{
lblError.Text = "Only .gif and .jpg files are allowed.";
}
}
else
{
lblError.Text = "Please select a valid file.";
}
}
catch (Exception ee)
{
lblError.Text = ee.Message;
}
}

/// <summary>
/// file upload using jQuery file selection mechanism
/// </summary>
private void FileUploadUsingJQuerySelectionMethod()
{
// check if file has been selected
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
if (file.ContentLength > 0)
{
string path = Server.MapPath("~/MultipFileUpload/");
string fileName = Path.GetFileName(file.FileName);

// now save the file to the disk
file.SaveAs(path + fileName);

lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully !<br />";
}
}




Responses

Posted by: Vuyiswamb on: 12/19/2010 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
To Store an Image to SQl you need to convert it to byte and store it, look at this

http://www.codeproject.com/KB/database/Store_images_in_SQL_Serve.aspx

Thank you for posting at Dotnetfunda
[Administrator]

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lalji_mer on: 12/19/2010 [Member] Bronze | Points: 25

Up
0
Down
Hi,
show this good article to upload multiple image file(Only .gif and .jpg files are allowed)using JQuery

http://www.dotnetfunda.com/articles/article981-multiple-files-upload-in-aspnet-and-aspnet-with-jquery-.aspx

LP MER

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 12/20/2010 [Member] Starter | Points: 25

Up
0
Down
thanks for your response,
but, i want
but i want to store file/images in database.
so please correct this code.

thanks.

protected void jQueryUploadFiles(object sender, EventArgs e)
{
FileUploadUsingJQuerySelectionMethod();
}

/// <summary>
/// file upload file using ASP.NET FileUpload control
/// </summary>
/// <param name="fileUpload"></param>
private void UploadFileUsingASPNET(FileUpload fileUpload)
{
try
{

// check if file has been selected
if (fileUpload.HasFile)
{
string path = Server.MapPath("~/MultipFileUpload/");
string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);

// check for the valid file extension
string fileExtension = Path.GetExtension(fileName).ToLower();
if (fileExtension.Equals(".gif") || fileExtension.Equals(".jpg"))
{
// now save the file to the disk
fileUpload.SaveAs(path + fileName);

lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully ! <br />";
}
else
{
lblError.Text = "Only .gif and .jpg files are allowed.";
}
}
else
{
lblError.Text = "Please select a valid file.";
}
}
catch (Exception ee)
{
lblError.Text = ee.Message;
}
}

/// <summary>
/// file upload using jQuery file selection mechanism
/// </summary>
private void FileUploadUsingJQuerySelectionMethod()
{
// check if file has been selected
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files;
if (file.ContentLength > 0)
{
string path = Server.MapPath("~/MultipFileUpload/");
string fileName = Path.GetFileName(file.FileName);

// now save the file to the disk
file.SaveAs(path + fileName);

lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully !<br />";
}
}

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response