I dragged a file upload control to my Form, and I want to save the "image" the user chooses to an object[] Array so I can then save it.
I only need to know how to "grab" the image the user selects and save it as a byte[] array.Thanks
I would use the fileupload.SaveAs Method
if(myFileUpload.HasFile)
{
myFileUpload.SaveAs(filenmae);
}
Would you not consider outsourcing to gravatar, like in SO ?
Related
Question on the NWindLayout demo
dxdemo://Win/XtraGrid/MainDemo/NWindLayout
scrin1
scrin2
How to place an image in the field?
Do I need to store the picture in the database?
or
The picture is stored on a local disk, and the database stores a link to the picture and the "Photo" field displays the photo according to the link?
As far as I understand, you have a column in your database, whose data are strings representing paths to images. And you are assigning PictureEdit as an in-place editor for the column. If so, the approach with using an unbound column is recommended since the PictureEdit editor does not provide the capability to show an image by setting a string path as an editor's value:
void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
GridView view = sender as GridView;
if(e.Column.FieldName == "Image" && e.IsGetData) {
string fileName = view.GetRowCellValue(view.GetRowHandle(e.ListSourceRowIndex), "ImagePath");
e.Value = /* get image from cache by filename or load image from file and add to cache */
}
}
Take a look at the How to display external images in Grid if its data source contains links to the images example to see this approach in action.
I'm working on a photo gallery using ASP.NET.
I'm storing user's images in a SQL database. I'm not sure how should displaying images look like.
Let's say there is 1 picture per user, I was doing something like that:
get image from database
save it on server's disc as "file.jpg"
ASP:Image.uri = "file.jpg"
And that worked fine until I found out that If few users loads that page at the very same time, It might not work properly.
Then I though changing "file.jpg" into some random string would help me:
get image from database
save it on server's disc as "ABCDUDHDJSAKFHADGJKHAKADFAD.jpg"
ASP:Image.uri = "ABCDUDHDJSAKFHADGJKHAKADFAD.jpg"
File.Delete("~/ABCDUDHDJSAKFHADGJKHAKADFAD.jpg");
But it wasnt possible to delete this file because it was still being used by a server.
What would be the proper way to solve my problem? User in my photo gallery will eventually see 12 photos at the same time.
What I usually do is serve images from DB using a generic handler (ashx file).
What you need to do is create an ashx file and use something along these lines:
context.Response.BinaryWrite(myImage);
The you create image tags like so:
<img alt="whatever" src="/images.ashx?iid=1243 />
Where iid is the unique ID of the image in the DB.
You should not store the file actually. You should stream it. For example create a handler which returns the image from an user with a specific ID and call it with that ID like image.ashx?id=1. The handler could look like
Image image; // image from your database
using(MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.WriteTo(context.Response.OutputStream);
}
Response.ContentType = "image/png";
This handler you can use like a static image file. So, you might even use it for a background-image:
<div style="background-image: url(image.ashx?id=1)">Username</div>
hi,
I am facing a difficulty in understanding this example. from where the asp:repeater is taking the product images? and what is this path?
my questions are:
1) From where the background-image is taking the images, and what is this path?
2) How can I store Images in the database and assign them to each product accordingly (Binding the images with the products using the product_ID for example?
Code:
background-image: url('<%# Eval("ProductID", "../../../Img/Northwind/Products/{0}.jpg") %>');">
Best Regards.
About your question.
Technically, the repeater is not 'taking' images. It is generating CSS, with a reference to the background image. The browser displaying the page is then responsible for resolving the path and retrieving/displaying the images. In this case, the images are located 'up' a few directories from the location on which the page is found.
For serving images stored in a database, you have a few options.
The most common approach is to build an image handler; a simple ashx generic handler does the trick nicely.
Make the handler accept a productId via query string, grab the blob from the database using the productId, then write the blob with correct content type out to the response stream.
Once the handler is complete, you can reference the images by referring to the handler:
background-image: url('<%# Eval("ProductID", "/ImageHandler.ashx?ProductId={0}") %>');">
Edit: In response to your comment, a quick and dirty handler would look similar to this.
Obviously, you would need to add the actual logic to get the blob from the database.
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int productId;
if (!int.TryParse(context.Request["productId"], out productId))
{
context.Response.End();
return;
}
byte[] blob = null; // get blob from DB
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(blob);
context.Response.End();
}
public bool IsReusable { get { return false; } }
}
1) From where the background-image is taking the images, and what is this path?
This is referencing to a physical folder on the hard drive. The folder has a collection of images called 1.jpg, 55.jpg etc which match up to the product id. In this sample they were probably just created manually but you could build an image upload system that would save image files with the same name as the product id of your products.
2) How can I store Images in the database and assign them to each product accordingly (Binding the images with the products using the product_ID for example?
Storing images in the database is different to whats going on here. As I say, this is just building a path to a file which has the same name as the product id. You -can- save your images in the database itself but I think this is a different discussion to the one you are looking at.
Basically the answer to your query is that in the sample they don't show you how to build a system which will let you add products to the database and upload images. It just shows you how to build some html based on existing database info and existing images.
I am using asp.net 3.5 and C#.
I have a image which I want user can download.
Like, there would be a download button or link. When user click on this link he will be prompted with a pop up to save that image to his desktop.
I have tried with
<a href ="path" > </a>
but it is opening the image in other page, I want user to be prompted to either save or view the image,
please help
Thanks in advance
You need to write an IHttpHandler that serves the image along with a Content-Disposition header.
For example:
Response.AppendHeader("Content-Disposition", "attachment; filename=\"MyImage.png\"");
Response.TransmitFile(path);
You would probably pass the image name on the query-string.
If so, make sure it doesn't contain / or \, or attackers will be able to read arbitrary files.
You need to have another page or, better yet, an HttpHandler, that takes the image path as part of the query string or as a post parameter that will send the response with Content-Disposition set to attachment. Setting the content disposition this way will cause the browser to display the file download dialog. A slightly easier way, though it depends on the user doing something extra is simply to have the link open the image in a new page and let the user right-click on it and do a "Save As".
Download
or
<a href="/path/to/image" target="_blank">
Load Image in New Window then Use Save As</a>
In my web application, I need to be able to allow users to upload and download their images. How can this be don in ASP.net?
I want to user to be able to sign in (I already have that done) and be able to upload images to their account. Later on, I want them to be able to download them.
Thanks
If the images are of a reasonable size, (e.g. less than a few MB), you can do this with a <input type=file> tag. Set the encoding of your form tag to multipart/form-data and implement some code-behind to get a hold of the file.
See the ASP.Net videos (Part 1) and (Part 2) for a detailed example.
Hope this helps!
You can also use
<asp:FileUpload runat="server" id="upImage" Width="575px" CssClass="txt-Normal"></asp:FileUpload>
Here is my method to save them to a location on the site.
string completeFilePath = this.Request.PhysicalApplicationPath + System.Configuration.ConfigurationManager.AppSettings["ImageUploadPath"];
if (System.IO.Directory.Exists(completeFilePath))
{
System.IO.Directory.CreateDirectory(completeFilePath);
}
if (!System.IO.File.Exists(completeFilePath + this.upImage.FileName))
{
this.upImage.SaveAs(completeFilePath + this.upImage.FileName);
}
imageUrl = string.Format("~{0}{1}", System.Configuration.ConfigurationManager.AppSettings["ImageUploadPath"], this.upImage.FileName);