IRepository
public interface IRepository
{
T GetById(object id);
List GetAll();
IQueryable Query(Expression<Func> filter);
void Add(T t);
void Delete(T t);
void Save();
}
public interface IRepository
{
T GetById(object id);
List GetAll();
IQueryable Query(Expression<Func> filter);
void Add(T t);
void Delete(T t);
void Save();
}
function updateChat(supplier_id, user_id, spantag) {
$.ajax({
type: "POST",
url: "AjaxService/ChatService.asmx/GetChatBySupplierID",
data: "{'supplier_id':'" + supplier_id + "', 'user_id':'" + user_id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = ($.parseJSON(response.d));
var str="";
for (var i = 0; i < result.length; i++) {
str += "[" + parseJSONDate(result[i].chat_create_dt) + " " + result[i].usr_fname + "] " + result[i].chat_text;
str += "
";
}
$("#" + spantag).html(str);
setTimeout(function () {
updateChat(supplier_id,user_id, spantag)
}, 3500);
}
});
}
function parseJSONDate(jsonDate) {
var d = parseFloat(jsonDate.slice(6, 19) );
return new Date(d).format("MM/dd/yyyy hh:mm:ss tt");
}
public class FileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = HttpContext.Current.Response;
HttpRequest Request=HttpContext.Current.Request;
Response.Expires = 0;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (Request.QueryString["fid"] != null)
{
ProductItem pi= new ProductItemRepository().GetById(Request.QueryString["fid"]);
if (pi != null && pi.pi_image != null && pi.pi_file_name != null)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + pi.pi_file_name.Trim());
Response.ContentType = "application/octet-stream";
byte[] buffer = pi.pi_image.ToArray() as byte[];
if (Request.QueryString["t"] != null)
{
string t=Request.QueryString["t"];
int n;
Int32.TryParse(t, out n);
if (n == 1)
{
//Converts the byte array to memory stream, then resize the image, then convert back
using (MemoryStream ms = new MemoryStream(buffer))
{
using (Image image = Image.FromStream(ms))
{
using (MemoryStream stream = new MemoryStream())
{
Image img = image.GetThumbnailImage(40, 40, null, new IntPtr());
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
buffer = stream.ToArray();
}
}
}
}
}
Response.BinaryWrite(buffer);
}
}
Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
Aneef has a great tutorial on uploading binary files to the db using Linq. http://www.aneef.net/2009/01/16/uploading-binary-files-or-images-using-linq-to-sql/comment-page-1/#comment-100
Here’s my version using only Linq.
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fuFileUploader.HasFile && fuFileUploader.PostedFile.ContentLength > 0)
{
string path_file_name = fuFileUploader.PostedFile.FileName;
string ext = Path.GetExtension(path_file_name).Replace(".", "");
string file_name = Path.GetFileNameWithoutExtension(path_file_name);
string file_title = txtFileTitle.Text.Trim();
HelperDataClassesDataContext db = new HelperDataClassesDataContext();
try
{
byte[] file_byte = fuFileUploader.FileBytes;
Linq.Binary file_binary = new Linq.Binary(file_byte);
ControlDocument cd = new ControlDocument
{
guid = Guid.NewGuid(),
file_ext = ext,
file_nm = file_name.Trim(),
file_title=file_title,
file_bin = file_binary,
is_active = true,
upload_page_type = rblLocation.SelectedValue,
upload_dt = DateTime.Now,
upload_by = UtilUniverse.Common.CurrentUserLogin.Trim()
};
db.ControlDocuments.InsertOnSubmit(cd);
}
finally
{
db.SubmitChanges();
}
}
}
your sample code is very useful and it works properly for me.
I am trying to do simple social network website in my campus.For the website i need users to upload phto and display on their page.Previously I use Handler.ashx page to display image from a database using sql command.when i try to change the code written in sql command to linq i got a problem.If you know how to display the image stored in database send me.
here is my email :afework1980@yahoo.com
thank you for your help!
NIce article very informative
My dog Onion barfed on our carpet today. What am I supposed to do? I was ready to put him in his cage, but he was so quiet that I left him playing in the sun…more like lying in the sun.
Sometimes your clipboard will get full of copy and pastes. To clear it:
The word “Bouh” means a lot to a family who follows “Theravada Buddhism”. I did this to thank my parents and oh, were they so proud to be parents. In my heart I did the best thing any child could do for their parent, family, and friends. It’s a small sacrifice I’m willing to make. Here’s my schedule as a monk. I was there for two weeks-all the vacation I had left.
Schedule
4:30 AM – Wake up and get ready to chant
5:00 AM – Chant ![]()
6:00 AM – Meditate
7:00 AM – Breakfast
8:00 AM – Relax, read, meditate, clean, chores
11:00 AM – Lunch
Some pictures:
Hello Sir R,
Are you able to send this newbie your sample code via email?
I would like to see how you used HelperDataClassesDataContext. Also, your code got cut off the side. Thank you in advance for sharing your talents.