How To Upload a File in MVC3? - asp.net-mvc-3

HI all i have a situation like this ......
AttachmentName:[TextBox] [FileuploadControl] Add
[Upload]Buton
when the user Gives the FileName and Uploads the file name and clicks on add i want to display the Attachment Name in a GridColumn and when he is finished and clicks on Upload button i want to save the file in this path
string savefilename = Path.Combine(Server.MapPath("~/Upload"),
Path.GetFileName());
and i want the uploaded file name to be Attachmentname entered in the textbox...and i wan the url of uploaded file to be saved in database...can any one give me any idea how to do this

Related

download pdf files that are href links using ruby mechanize

Using Ruby Mechanize I have successfully submitted input values to a form and am able to get the resultant page based on the search criteria. The resultant page has pdf files as ahref links that i need to download.
Attribute href has value:
href='xxx.do?FILENAME=path/abc.pdf&SEARCHTEXT=aaa&ID=123_4
where SEARCHTEXT is the text entered as input originally. When i manually click the link pdf opens in a new window having
url as http://someip:8080/xxx/temp/123_4 which is the same ID seen in the href attribute. The actual filename however is different and is of the form xxx.123_2_.doc. My below code returns 0 byte file -
scraper.pluggable_parser.pdf = Mechanize::FileSaver
File.open('n1pdf.pdf', 'wb'){|f| f << scraper.get(alink).body}
where alink=http://someip:8080/xxx/temp/123_4
If i use
File.open("new.pdf", "w") do |f|
uri = URI(alink)
f << Net::HTTP.get(uri)
end
I get HTTP not found error.
I am not sure if i am doing this correct. Is ID a session id that is generated dynamically since all pdf files on the resultant page have this ID with _1/2/3 as filename(or url).
Please note that whenever i manually click and open a pdf file and then hardcore that in my code the file downloads but does not when my code dynamically extracts the ID value and assigns to alink. Not sure if this is related to cookies. Kindly help. Thank You.
Make sure it's the right absolute url:
uri = scraper.page.uri.merge(a[:href])
puts uri # just check to be sure
File.open('n1pdf.pdf', 'wb'){|f| f << scraper.get(uri).body}

fineuploader image uploads include caption with uploaded files

I have a legacy app that has five separate file uploads for a single DB record. Beside each file upload there is a field to enter a caption for the uploaded file.
I am considering replacing the whole lot with a fineUploader gallery and allow up to ten files to be uploaded.
However, it was useful on the old system to have a caption with each image for the ALT tag of the image when it comes to web display.
I could address this with multiple single file uploads using fineuploader and a caption field for each but I want to get away from having so many on the page.
I see there is an option to change the file name during upload so that might be an option but that could lead to very long/messy file names and may cause issues with accents and other characters.
Can anyone suggest a good approach?
I would suggest considering you use the built-in edit filename feature, as this seems most appropriate to me and will certainly be the simplest approach.
Another approach involves the following:
Add a file input field to your Fine Uploader template. This will hold the user-entered caption value. You will likely need some CSS as well to make this look appropriate for your project.
Initialize Fine Uploader with the autoUpload option set to false. This will allow your users to enter in captions and then upload the files by clicking a button (to be added later).
Register an onUpload callback handler. Here, you will read the value of the associated file's caption stored in the text input and tie it to the file with the setParams API method.
The file list portion of your template may look something like this:
<ul class="qq-upload-list-selector qq-upload-list" role="region" aria-live="polite" aria-relevant="additions removals">
<li>
...
<input class="caption">
</li>
</ul>
And your Fine Uploader code will contain this logic (important but unrelated options such as request.endpoint and element left out to maintain focus on your question):
var uploader = new qq.FineUploader({
autoUpload: false,
callbacks: {
onUpload: function(id) {
var fileContainer = this.getItemByFileId(id)
var captionInput = fileContainer.querySelector('.caption')
var captionText = captionInput.value
this.setParams({caption: captionText}, id)
}
}
})
Your server will receive a "caption" parameter with the associated value as part of each file's upload request.

How to upload image on server's image folder

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.

CodeIgniter retrieve image from secret folder

I have create private folder in application folder: application/private/username, I'm uploading images here with ajax, that works, but how can I retrieve that image with ajax, and display it in img tag. First problem is that http://example.com/application/private/useraname/img.jpg is protected, I can't access it through out url in browser, how to display an image. Just advice, I wanna write code on my own.
This is part of function(that is part of controller) that grabs image:
$filepath = 'path to image';
header("Content-type: image/jpeg");
if (file_exists($filepath)) {
$img_handle = imagecreatefromjpeg($filepath) or die("");
echo $img_handle;
ImageJpeg($img_handle);
}
But how to get this image with ajax?
You might want to create a PHP file that would get the filename eg thumb.php?src=img.jpg and inside that file, you load the image from that path and display it. This way the path of the images are not exposed to the user.

ajax function which uploads the image and preview it with another string from server

I want to use file upload control to upload an image and preview it in same page without submitting the form, is this possible?
if possible, please let me know how to set the query string of the ajax function, and how to handle that query string at server side. I am using jsp or servlet.
Intention is to send the image through ajax function call, where I can store the image in server's local folder. also the form will be updated with the part of the result string which is some other component output. Along with this I can show the image which user uploaded.
1. upload the image through ajax call.
2. return string is do two things, one for shows the image in respective place holder by servers local path and another is to get the other component output which is a string.
3. if i have that string, I am manipulate at call back function to set the values.
4. after enters users data I want to submit the form finally.
I tried it, but failing to send the file object as parameter to the servlet or jsp. Also if we can send that object, how can we handle that object at jsp?

Resources