This is my spring project structure.
I have to upload hotels picture in image folder. But i am not getting path of image folder.I tried below code
//There is some code here
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();
itemName = item.getName();
File savedFile = new File(request.getRealPath("/")+"image-folder\\"+itemName);
item.write(savedFile);
}
but it is giving error : Directory not exist.
Now I have two question.
What is solution of above problem?
Is this a right way to upload image in server's image folder? Because it could be temporary i have read on stackoverflow?
Edited:
Runing project from eclipse and when i printed path it gives
E:\DeepakWork\current\jmx_examples.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\HotelManagment\image(file-name)
I first want to answer the second most important question
Is this a right way to upload image in server's image folder?
No, it is not. Because it is a security issue! Never ever upload any user generated content in the web application folder!
Instead upload the files in some fixed (configured) folder somewhere outside of the Tomcat Directory.
Related
I am developing a form application with monodevelop. I'm using gtk#.
I need to add an image to a widget in the form. I am also trying to make this image an embedded resource as well, before including it in the form.
so far this is what I have:
HBox CharacterPic = new HBox();
Image LegionnairePic = new Image('somehow load the embedded resource image here');
CharacterPic.PackStart (LegionnairePic);
In the 'Solution' section to the left, I have added .jpeg files and changed their 'build action' to 'embedded resource'. However, I cannot access/load them onto the form them as so:
Image LegionnairePic = new Image(<namespace>.<resource>);
How do I add the image resource to the form? Am I even adding the resource correctly?
I believe you have to access the embedded files with the following method:
// string resource_id is the Resource ID of the file in the sidebar “Properties”
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource_id);
This returns the resource file as a Stream.
If this helped, thank that guy like I did :)
I have been trying to import products into Magento and images. The products are being imported and created properly but the images to not get imported.
I am aware of the basics of importing as I have done it in the past but cannot figure out what the issue is and what to do next.
some notes how and what I did:
I am aware to add a "/" before the image name
image name is case sensitive
upload images in media/import folder
media folder to be 777
Tried different folders like var/import or
media/catalog/product/import
Removed the .htcaccess file in media
Flushed cache
when i upload manually an image on a product it does show up properly
i tried specifying the _media_attribute_id in a column as 88 maybe
that should help but it didn't
If you have a Mage_Catalog_Model_Product, all you need to do is this:
$fn = '/absolute/path/to/image.jpg';
$types = array('thumbnail', 'small_image', 'image'); // or null if you already have the product's thumbnail, small image, and image set
$move = true; // move the source file, don't leave it sitting around
$exclude = false; // enabled in product view
$label = 'Turquoise';
$product->addImageToMediaGallery($fn, $types, $move, $exclude); // Strictly speaking, only the first parameter is required
if(!empty($label)) {
$gallery = $product->getData('media_gallery');
$gallery_img = array_pop($gallery);
$gallery_img['label'] = $label;
array_push($gallery['images'], $gallery_img);
$product->setData('media_gallery', $gallery);
}
$product->save();
I have faced the same problem while uploading product image. And i tested many methods to figure that out. But nothing helped me. At last in one of the tutorial I have seen that the image name and the sku name has to be same. with the extension of file extension. So i followed it and by God's grace it worked. You may try that also. It worked for me. I had spent almost one full day to figure that out.
I a working on a simple window phone application. In which I have two filed Name and Images.
I want to save this item and want to view all saved data.
Now My query How to select images to save? and How to save images and also get images to view.
I also used PhotoChooserTask but How to save selected image and how to get saved images?
I know about how to save image file in Isolated storage. But how to save selected images and get all data?
Thanks,
Hitesh.
Thanks for your reply. I knew about photoChooserTask. I also save my image file in isolated storage. But I dont know what is the images path to save images path in database and how to display all those images in datagrid. I have a table which have fields like ID, Name and Image path. I dont know what to save in imagepath filed if I saved image in isolated storage and how to display all data in datagrid.I used following code to save data into database. IN below code please correct the image path if I was wrong.
CategoryVO newCategory = new CategoryVO()
{
Name = txtCategoryName.Text,
ImagePath = txtCategoryName.Text.Trim() + ".jpg"
};
Expdb.Category.InsertOnSubmit(newCategory);
Expdb.SubmitChanges();
Using the PhotoChooserTask you can actually launch the photo chooser application and handle the selected image.
If you want to integrate this in your application, create the instance of the PhotoChooserTask and call the Show() method. If you want to handle the user’s selection, register the Completed event which will give you handle of the chosen photo.
var photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += PhotoChooserTaskCompleted;
photoChooserTask.Show();
In the completed event implementation, you can get the chosen image as PhotoResult and set the image to your Image control or can use it in other places.
void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{
switch (e.TaskResult)
{
case TaskResult.OK:
imageChooser.Source = new BitmapImage(new Uri(e.OriginalFileName));
break;
}
}
Source: http://www.codeproject.com/Articles/350126/How-to-use-the-PhotoChooserTask-to-Launch-the-Medi
I'm developing an application where the user can add photos from windows phone 7 photo library and assign them to a particular view. To do this I save the OriginalFileName on the database (LINQ to SQL). Later I want to recover the photo and load it into the view. Do you know what I can do? currently I have this code but does not work.
When the user has selected the picture I keep his name in the variable fileName:
private void photoChooserTask_Completed (object sender, PhotoResult e)
{
BitmapImage image = new BitmapImage ();
e.OriginalFileName = fileName;
image.SetSource (e.ChosenPhoto);
this.Thumbnail.Source = image;
this.Thumbnail.Stretch = Stretch.UniformToFill;
}
Later, when the user wants to save this setting I save the fileName in database.
This is the code when I load the view that must contain the photo.
imgSource var = new BitmapImage (new Uri (picture.Url, UriKind.Absolute));
item.LeftImage.Source = imgSource;
Where picture.Url contains the filename.
Any idea? I saw on the internet that you can keep the whole image, but give it the best possible.
What you should do is save the picture returned from the PhotoChooserTask in the IsolatedStorage.
You will then be able to load it when needed.
Here is how to Read and Save Images.
For what you need is to get the picture by browsing the MediaLibrary without using PhotoChooserTask, because as you experienced, the file name might not be the same if you use different methods.
For the custom MediaLibrary browsing interface, you could refer to this codeplex project:
https://multiphotochooser.codeplex.com/
I've tried adding a image via the following however it is still not working. The image type is a content.
Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("myimage.png", UriKind.Relative));
//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.Fill;
image.Width = 40;
image.Height = 40;
// Center the image around the location specified
//Add the image to the defined map layer
phoneDetailsLayer.AddChild(image, e.Position.Location);
mapViewAll.Children.Remove(phoneDetailsLayer);
mapViewAll.Children.Add(phoneDetailsLayer);
Make sure that your image is the correct resource type and is loaded optimally (ie once if being used multiple times). There are multiple approaches to loading images for WPF (same as WP7) which are described here: WPF image resources
This post here: Visual Studio: How to store an image resource as an Embedded Resource? discusses the different image resource types you should/shouldn't use.
I think you should have a look at both as its a good thing to understand, as it can help you to avoid issues in the future that could pop up.
I can't add a comment to your question, however I'll ask here when you say content, have you added the image directly to the project containing your code or to a separate content project?
Assuming that you have added it directly:
If you had set the "Build Action" to "Resource" then you should use the GetResourceStream method:
Image image = new Image();
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
However if you have set the "Build Action" to "Content" you should use the GetContentStream method
Image image = new Image();
StreamResourceInfo resource = Application.GetContentStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
Just to clarify the answer to this questions. The problem was not in the resource type, the problem was related to the way relative Uri's work. Just like any well structured project ericlee used different folders within his project like this (relative to the project root):
/pages - Contains the actual pages and therefore also the page containing the above code
/images - Contains the actual PNG images that have to be referenced
In the original code a reference is made to "myimage.png" as a relative uri. The app will now look at "/pages/myimage.png" and therefore won't find the image. The trick here is to use the correct relative URI. It can be constructed as follows:
1. First go up to the project root by using two points -> .. (one for the current dir, one extra to go up one level)
2. Now reference /images -> ../images
3. Now add the actual file reference -> ../images/myimage.png
If you use the correct URI the problem is solved.
The main question seems to be how to get true uri.
For me, the following table helps me in this case (I only have it in German):
http://msdn.microsoft.com/de-de/library/aa970069.aspx
example:
// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
// Relative URI
Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);
example 2:
Uri uri = new Uri("pack://application:,,,/File.xaml");
or Codebehind:
'Image compiling is set to "content"
MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example
/projectname;component/images/menu/lost.png
Is the correct way, the rest of your answer is really not working