با تشکر از همه دوستان
گام اول :
کد HTML:
<input type="file" id="f_UploadImage" /><br />
گام دوم :
var formData = new FormData(); formData.append('file', $('#f_UploadImage')[0].files[0]);
$.ajax({
type: 'post',
url: 'FileUploader.ashx',
data: formData,
success: function (status) {
},
processData: false,
contentType: false,
error: function () {
alert("Whoops something went wrong!");
}
});
گام سوم :(Genericholder)
using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
namespace SwapAgreements.WebForms
{
/// <summary>
/// Summary description for FileUploader
/// </summary>
public class FileUploader : IHttpHandler
{
connction objcon = new connction();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
try
{
string dirFullPath = HttpContext.Current.Server.MapPath("~/MediaUploader/");
string[] files;
int numFiles;
files = System.IO.Directory.GetFiles(dirFullPath);
numFiles = files.Length;
numFiles = numFiles + 1;
string str_image = "";
string plan_number = context.Request.Form["plan_number"];
string Uniqno = context.Request.Form["plan_no"];
foreach (string s in context.Request.Files)
{
HttpPostedFile file = context.Request.Files[s];
string fileName = file.FileName;
string fileExtension = file.ContentType;
string name = Path.GetFileNameWithoutExtension(fileName);
fileExtension = Path.GetExtension(fileName);
int size = file.ContentLength/1024;
Stream fs = file.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
if (!string.IsNullOrEmpty(fileName))
{
//save in logic drive
str_image =name+fileExtension;
string pathToSave_100 = HttpContext.Current.Server.MapPath("~/MediaUploader/") + str_image;
file.SaveAs(pathToSave_100);
//insert in database as binery Code
PersianCalendar pc = new PersianCalendar();
string dateYear = pc.GetYear(DateTime.Now).ToString();
string dateMounth = pc.GetMonth(DateTime.Now).ToString();
string dateDay = pc.GetDayOfMonth(DateTime.Now).ToString();
string date = dateYear + "/" + dateMounth + "/" + dateDay;
SqlConnection connect = objcon.conect();
SqlCommand com = new SqlCommand();
com.Connection = connect;
com.CommandText = "INSERT INTO tbl_attachments(planproject_no,plan_number,attach_ file_name,attach_file,attach_file_type,attach_file _size,attach_file_date) " +
"VALUES(@_planproject_no,@_plan_number,@_attach_fi le_name,@_attach_file,@_attach_file_type,@_attach_ file_size,@_attach_file_date)";
com.Parameters.Clear();
com.Parameters.Add("@_planproject_no", SqlDbType.UniqueIdentifier).SqlValue =new Guid(Uniqno);
com.Parameters.Add("@_plan_number", SqlDbType.NVarChar).SqlValue = plan_number;
com.Parameters.Add("@_attach_file_name", SqlDbType.NVarChar).SqlValue = name;
com.Parameters.Add("@_attach_file", SqlDbType.VarBinary).SqlValue = bytes;
com.Parameters.Add("@_attach_file_type", SqlDbType.NChar).SqlValue = fileExtension;
com.Parameters.Add("@_attach_file_size", SqlDbType.Int).SqlValue = size;
com.Parameters.Add("@_attach_file_date", SqlDbType.NVarChar).SqlValue = date;
connect.Open();
com.ExecuteNonQuery();
connect.Close();
}
}
// database record update logic here ()
context.Response.Write(str_image);
}
catch (Exception ac)
{
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}