Trouble with uploaded files in ASP.NET - c#

I know how I can upload a file (i.e. PDF file) in ASP.NET. How can I do some operation on this file after it had been uploaded?
For example, I need to correct the wrong words, or another application is for some plagiarism system.

Related

Upload File from local computer to server c#

Is there any way to upload a PDF file that is on my local computer in a way where anyone can retrieve the PDF?
If I do something like
var filename = #"c:\test.pdf";
m.Attachments.Add(new Attachment(filename));
Anyone who goes onto the website has to have the same local path and same test.pdf file in the correct place (which would be impossible). So is there a way I can go about having to upload the pdf so it's not hard coded with a local path?
There are many ways to upload a file e.g. using an HTTP request and then download a file again using another HTTP request. If you just need to download files (no other API endpoints), consider using blob storage like AWS S3 bucket, Azure Blob Store or equivalents of other providers directly and configure the access rules in such a way that public read access is possible. To download the file in your program you then just need an instance of HttpClient and make a request to your blob store.
If you want to upload files through ASP.NET have a look at the Microsoft docs which describe all things to be considered in detail. You will still need to store the file somewhere when using this approach. Either on disk of the server you run the application on or again a blob store (and there are of course other possibilites).

Most reliable way to validate an uploaded file type in C# to prevent malicious, even if renamed to accepted file extension

I read some articles about uploading files, people can upload malicious programs (php, exe,...) to attack the server and the website, what is the reliable way to protect and avoid any danger for the website and our server. In my website, I need to allow my registered users to upload files like
.doc, .txt, .pdf extension files.
So any idea to validate to uploaded file is in acceptable file format (even it is renamed)
Ideas in C# will appreciated.

How to open a local file from a web client/server

I'm trying to add a link in a project that will open a tutorial that explain how to use the system.
The tutorial is only relevant to those who work in the company so I figured it would be best to place the the tutorial on our company network drive and only redirect to this file from web client. In that way it will also be easy to change the tutorial without reuploading the system.
The problem seems that chrome is blocking me from opening a local file from the web client.
But if I remember correctly there are web sites that can open a local pdf files. so why I can't open the tutorial? It is basically a presentation (I think in flash, not really important) that you can open by opening a html file.
Is there any alternate solution for this?
I'm also okay if chrome will ask the user if he is sure that he wants the file to be opened or something like this.
But I don't want to add files directly to my project code because it will require reuploading the whole project to only update the tutorial.
I'm using ASP.NET Framework in the backend and Angular in the Frontend.
No person with a brain would use a browser that lets it open local files. You mean you come to my site to watch cat videos and all the while the browser is opening your banking files or stealing your excel sheet marked "my passwords?". not even close! A browser cannot open local files period!!! To high of a security risk.
You can of course EXPOSE a folder to the web server, and then ANY file in that folder becomes part of the web site and MORE important becomes part of the web server "URL" mapping.
So, you can have your typical site (in say inetpup\wwwroot. In that folder then ONLY valid urls to files in that web site folder are allowed.
And say you have some big file server on your network full of files? Well, then you can add to the site what is called a virtual folder.
So, say the web site is on
c:\inetpub\wwwroot
So, any valid URL typed in a by a user (or your code launching a web page) is now
http://mysite/default.aspx
The above file of course maps to
c:\inetpub\wwwroot\Default.aspx.
So, say you have a folder on the network full of pdf's you want users to see/view/use?
Well, say that folder is:
\SERVER5\PpdFileArcive.
So, that folder is NOT part of the web site. What you do is add a virtual folder to the asp.net site. Lets call it MyPdfs
You map MyPdfs to the above \SERVER5\PdfFileArchive
So, now your URL becomes this:
http://mysite/MyPdf/help.pdf
So, the browser can NOT look at local files on your computer (and would you actually think that is ok that my web site can rummage around on YOUR local computer? Not!!!!).
So you can have some folder sitting on your local network. And if the web site is on that SAME network, then you can add a virtual folder to the web site. That "external" folder will then become part of the web site and mapped to a web "URL" that will then allow you to say have hyper-links, or even allow the user to type in ANY url like:
http://mysite/MyPdf/HowToCookPotatos.pdf
In fact, in many cases you do NOT want users to type in URL's, and in often you do NOT want the users to be able to type in a url.
Well, keep in mind that code behind (the .net code running on the web server) is really the same as .net desktop code. That code behind can open/read/use any file anyplace on the network - including files on your desktop. But that would assume the web server is on the SAME network as you, and it would assume that say even a desktop program running on ANY computer has rights to YOUR desktop folder. (and that's not the default - I think most desktops have a shared public folder).
So, code behind on that web server can open, read/process any file. But you NOT be able to say use a hyper-link, or say a valid URL to get at those files.
Since a company often does NOT want to expose that big huge pdf folder to the wild and crazy internet? Then what is common done is that you do NOT create a virtual folder, and you do NOT map URL's to that internal company folder. you have the code behind OPEN + READ and then STREAM the file down to the browser. You can google and find 100's of examples - just google stream a pdf to browser for how this works. But again, keep in mind that this trick/suggestion STILL is limited to code behind running on the server being able to directly access and open that file sitting on a folder. That file can be any place on the network as long as the web server has rights to read/open/use such files. Users of the web site will not have any possible URL to type in, but if the code behind has such rights or the ability to open such files, then web code can be written to "dish out" or so called "stream" that file to the browser.
So keep in mind the concpet of a web URL (a valid web path name), and that of code behind that does NOT use URL's to open and read files - but you use plane jane regular windows file path names.
Of course if you have a virtual folder and a URL exposed to end users, then code behind STILL often needs to process/open/copy or do whatever with that URL the user typed in. That's where server.mapPath comes in. it will translate the URL value to a full internal path name.
So
Code behind = ALWAYS needs a full valid windows path name.
URL -web folders (including virtual folders that point to server folders). These URL's can be used in hyper-links, web navigation, and even allows the user to type in a full valud URL to the given pdf in that folder (but the user will type in a valid URL that resolves to that folder).
So while you can't for all practical purposes have a browser read/get/use local files on a YOUR computer? You can certanly setup a folder on some server (even the web server) that has all those pdf's, pictures or whatever you want. And with a mapped virtual folder, then the Web users can then consume such files.
Or if you want to keep things locked down, don't want users typing in URL's to files that might not belong to them? Then you can of course maintain a list of files say in a database or whatever. And the code behind can read such files 100% directly with a full valid internal path name and then PUSH (stream) that file out to the user.
Here is a example of such code:
Stream PDF to browser?

local network get file path but not upload file

I know when uploading files for security reasons the browser will not get directory of file when using a file upload control. But what I am looking for is for the local network user to select a file in a network drive and capture the path and file name but not upload that file. this is just to store the file they select in a table. Is there a way of doing this? some sort of work around? the only thing I've thought of is putting a textbox and having the user copy/paste the file path but is there an easier method? again not uploading the file just capturing what file and location the local network user wants to use.
It can't be done using the File upload control (html input element). You have to use a plugin like (possibly) Flash or a custom ActiveX control.
What you are looking for is not possible using standard web technologies (html, css, js).

website to receive file upload from winform

I need to upload a file from a winform application to a website.
I have the winform side ready to go, I just need a little help with the webform side.
Client.UploadFile("http://xxx.yyy.com/data/", "sample.txt");
The file(s) that are to be uploaded, binary files, just need to be dumped into a subdirectory called data
I am stuck on the web page that will receive the file and save it into the directory.
c# and asp.net please
thank you
You don't need a web page for accepting the file being sent to server. Probably the easiest way would be to set up ftp on server and upload file there.
Sample code from msdn: http://msdn.microsoft.com/en-us/library/ms229715.aspx

Categories

Resources