Accessing Pdf file from Database using MVC4 Asp.Net - c#

I am stucked in a situation working from 2 days, tried searching many sites
for the solution couldn't find anything including Stackoverflow,
WHAT I NEED
I am working on MVC, and I have a Byte array file which is already saved in database(SQL) and I need to access the file from database, and display it into PDF file, there is not Writable task to do, just Readable from database.
WHAT I DID
->Wrote Javascript to get the ID's of the give information.
->on click of "Download" button called method in JS, and made redirect to Controller with the ID's I need (which is good till now!!) (PIC 1)
->Generated Stored Procedure to access all column information based on the ID's (PIC 2)
ScannedResult is the column where the value has been stored, and I need to access that file.
WHAT I COULDN'T FIND
-> Couldn't able to find a way to access that byte array and display into PDF!!
-> as in shown in PIC4, while debugging it shows the error message "Failed to convert parameter String to Int64", and I do know what does that error mean, I just want to give some more information here, the ID's I am getting is "BigInt" and I am passing it as String(see in Controller), Reason behind for this, there might be having multiple ID's in single shot, so using "," (PIC 3) in the middle of all ID's, so I have planned to convert into "String" or If any body have greater suggestion I am all yours to listen,
UPDATE1
Please help me to find out this issue, answers will be appreciated.

Related

C# easiest way of storing strings to external file

C#
TL;DR: I want the user to be able to input text, which is then written to an external file, which then can be called later based on position in the file. What's the easiest way and form to read and write a list of strings to?
Very amateur question, but I can't seem to find an easy anwer on the internet. If I'm missing something obvious, please redirect me. I am writing a very simple program in which the user can input a string which is then written (added) to an external file, from which later a string can be called based on the position in the file. I found things like JSON, Resource file, SQL DB... Problem is that due to my lack of programming experience I have no idea what's the best option to look into.
Example of what I want to achieve:
User inputs strings 'Dog', 'Cat', and 'Horse' into the textbox. Each of these strings are added to the external file. Lateron, the user calls the 2nd number on the list, to which the program returns 'Cat'.
Thanks!
If you already know the kind of data that will be saved I recommend using XML Serialization. This lets you save and read your file very easily. The linked example is from Microsoft and shows a dataset being serialized. If you want to save a generic list instead of a fixed object you might find this link helpful.
Alternatively, you could save data to your application configuration file (search online for "C# application configuration for PROJECT_TYPE" where the project type is winforms/mvc/class library etc..)

securely show images on website

I currently store a number of document preview images (jpg/tif) outside of my web root. There are 100s of them, so having this work efficiently is important.
The reason they are stored outside of the web root is that they contain data the only specific users/user groups may view (but each user can have 100s of documents they can view).
My current implementation is, when the user selects ‘view image’ an ajax call is triggered and this moves the image in question to a specific folder within the web root. The location is passed back and used to display the image to the user.
When the next image is clicked, the call deletes any existing images and copies over the requested image. At session logout / timeout the users image folder is emptied.
This has a few problems, but mainly:
Files are constantly being copied and deleted
There is the risk of images being left in the folder (issues with log off scripts)
The whole time an images is in the folder it could be viewed by another users (unlikely but possible)
Is there a better way of doing this? I looked at trying to combine the BinaryReader with the ajax call (as I hoped this would cut out the need to copy the files), but can’t see how to get the data back to be used by the JS in the calling page.
Alternatively is there a way of making selected Folders only accessible to given users based on some session criteria? (I can’t imagine there is but I thought it’s worth asking.)
So if anyone has any ideas on how this can be improved that would be great.
This is a c# ASP.NET app using Jquery.
Edit:
The image is displayed using ajax, this allows for preloading and also means the rest of the page does not need to be reloaded when they select the next/previous image.
It can almost be thought of as a javascript image swapper type situation, where the images are stored outside of the web root.
Thanks.
My current implementation is, when the user selects ‘view image’ an ajax call is triggered and this moves the image in question to a specific folder within the web root.
This is horrible idea. You realize you can just access the image data and pass it to web as stream with specific mime type, right?
Maybe try to write a method that will check user credentials by cookies, if it is not OK then load and send back some standard image that will say that user must log in to view file, if it is ok then load and show proper file from a path outside of root based on url parameter (with proper headers like content-type also often referred as mime-type ofc). Then link urls to that method with proper parameter(s).
You can easily find examples of code (like here) to display image in binary form from DB. You would need just to load them from some path outside of root, not DB.
Also you don't need to load it by AJAX - just add IMG with SRC pointing to URL of handler. Or redirect / open window if it needs to be downloaded not shown.
The issue was how to get an image to show via javascript that is not in the web root.
I created a generic handler (ashx file) that based on the session values (authentication) and submitted parameters would return an image.
That in turn is being called via AJAX.

How to persist a file between controllers using C# MVC?

I asked a previous question here and one part of it seems to be a whole subject in itself.
I am attempting to preserve a files data across controller calls.
The scenario is: A user uploads a file containing entries to be submitted to a database. If the file has warnings and errors they'll be redirected to a validation page. If there are no warnings or errors their file will be directly processed.
If the user receives warnings but no errors (ie. a text field is too long and will be truncated) they can still import the file. As there are two separate screens between uploading the file and sending it to the database the file data will need to be preserved.
I have seen a dozen similar questions to this but none of them seem to have relate-able answers (so please don't mark this question as a duplicate if a similar question doesn't have an answer to this exact problem).
My first idea was to save the file using the uploaded file name + a guid and saving that file name to the session state, but the answers I got on my previous question were all pretty adamant that this was a terrible solution.
The solutions I have found to other similar questions:
Reupload the file (a terrible solution)
Using a viewbag to send the file name to the view, to be sent back to the second controller (doesn't sound right)
Save the file location in the view model to be sent to the view, to be retrieved again by the second controller (again doesn't sound right)
Save the file location in the session or in temp data (receive generally unfavorable remarks)
I have done my research and cannot find the best way to go about this. Can someone please help me? If you think another question answers this, can you please send it to me as an answer instead of reporting this as a duplicate as I have already done quite a bit of searching.
Thank you,
Samuel Davis
EDIT: my users do have a session available. Another solution I could think of is appending a hardcoded string to the GUID contained within their session ID eg. "MyImporter-xxxx.xxxx.xxxx.xxxx.csv". This way "MyImporter" could be a private constant string, and my controller wouldn't have to persist any identifier. I am unsure of any downsides to this method, but as I am pretty fresh to MVC there should certainly be cases that I haven't thought of.
The best would be to store the location of the file on your server. Session or database.
Flow:
User uploads a file
The server stores the file in a some location on the server and stores the location of this file in a Session variable or in the Database
The user sends a second request
The server retrieves the location from the Session or the Database.
The server does something with the file
It's worth mentioning that if you decide to store the file location in a database, you should also store the currently authenticated user in the same table so that you could later retrieve the correct record. If you are not using authentication that might not be a good solution because you will not be able to retrieve the record. In this case you could use a hard to guess identifier in the database to store the file location under. Then this identifier will be passed to the view (under the form of a view model of course) and passed on subsequent request. It is important that this identifier is difficult to guess to avoid one user working with the files of another user.
Another possible solution might be to store the location of the file in an encrypted cookie which will be sent on subsequent request.
Have you heard of TempData? Its like a self-destructing session value.

ASP.NET MVC User input in C#

I am having the following issue, when a user fills in a form, I make an xml file based on that information and copy a file from one folder to another. (The filename is input by the user).
If the file already exists however, there will have to be a user input on whether to overwrite this file or not. I know I can't use a messagebox and if I do whether to use javascript or jQuery, I'd be adding a page as well. Preferably, I'd like to stay in C# but thats not possible without the messageboxes.
I have looked around google and stackoverflow, and the best I can think off is creating an extra page for this, where if the file exists, the user has to pick one of two options and then go back to moving the file (or not).
I am new to ASP MVC and I was wondering if this was the best way to go about it and what the best way to go is if it isn't.
Using JQuery you could make an ajax call to a controller to see if the file exists and show the user a dialog asking if they would like to overwrite the file if it exists. I can't see that you'd need a view for this at all.

How to Return A File and a strongly Typed data at the same time?

I am using asp.net mvc 1.0 and I want to return a XML file but I also want to return a strongly typed data back so I can update some fields.
Like the XML file will contain users who failed to be inserted into the database. So I want that to appear as a dialog save box what asp.net mvc return file() would do.
However I also want to return on the page like values like how many users failed to be added, how many users where added, etc.
So I want to use scafolding with the class file I want to pass it along. If this was a view I could pass it along as an object model but I don't see a parameter for that in File().
I also don't want to save the xml file onto the harddrive I want to do it through memory. So have a link that would display on the page to download the file and show the the data I want would not be desired.
I could be wrong, but I think you'll need to use JavaScript for this. What your're trying to do is a limitation of HTTP and from my understanding of HTTP, you can only return one file type per response since the protocol is a request->response.
You can have MVC return the strongly typed view in addition to the XML file name inside the ViewData. Then have a JavaScript function change the window.location property to the file's URL (or make a new window).
I'm not sure about the exact details on how to gracefully have the JavaScript spit the file out like alot of download websites have it.
Edit:
I found how to gracefully automate the download process, check this question out:
JavaScript automatic download of a file

Categories