There are a lot of JavaScript utilities now to allow posting a file in an AJAXified way to the server these days. Is there any utility that allows streaming bytes to the client and download a file? Or dues that still have to be a server-side solution? I'm using .NET.
Thanks.
There's no cross-browser support for accessing the filesystem of the client. You could probably do it with Flash/Java, but a much cleaner solution would be to do it in the server and create a download link for the user.
Brian: What you said about Javascript posting files to a server seems an incomplete statement. Only way javascript can post files to a server is by having the user manually selecting the file he/she wants to upload...
To answer your question...
You should be able to issue some sort of ajax call (to a web service, for example), have the web service read the file into a byte array and return it to the client. On the client side, you would need to assemble the byte array. I would assume you'll have to also set the appropriate response type from the web service call.
This seems to be something like what you are looking for.
Related
Is there anyone hehe who has experience with uploading very large files (3-5Gb) in Blazor and know if it works well, eg using c#, JavaScript manual shunk up files or HTML5 File API multipart upload? Preferably without a third-party library.
I also have a general question about the scenario of a logged in user if there are no special restrictions set for allowed file types possible to upload, what security concerns can still be handled using c#, JavaScript client, server side with regard to eg OWASP?
The only way I have gotten it to work properly is to make a native call to an API that is running on the same server.
You can use fetch or ajax to do so.
An example on how to use fetch: https://flaviocopes.com/how-to-upload-files-fetch
After it has been uploaded you can check the file location using dotnets file system.
You can always check the file extension however I would not recommend allowing just anyone to upload files to your website.
There is an API for virus total, I have not used it so look it through yourself!
https://github.com/Genbox/VirusTotalNet
You have to do it using pure javascript or jquery any attempt to do it using c# will use signalR and that has a very slow upload.
I have a URL to a mp3 file . I need to redirect to the mp3 file without showing the URL because it contains my API key; is there a way to do this?
I don't want the client/sniffer to read my URL.
Can someone please provide a code snippet that I can look at ?
Thanks
You cannot do this by redirecting the request, since a redirect will tell the client where to get the file.
The best solution would be to use a proxy application on a server where your users don't have direct access. This proxy software would then load the .mp3 from its original source and forward the data to your client. The API key you want to protect will never leave the server. However, you will need to control access to your proxy by some means.
If you are referring to a client side application: There will always be a possibility to get the URL out of the application. It depends on your threat model. Whom are you protecting the API key against? If you have highly skilled attackers in mind, it's likely that they will be able to extract the key from your application.
Anyway it is a good idea to use encrypted connections when transferring credentials. If you are using https:// and checking the certificates you will at least defend yourself against sniffing the network traffic.
Actually there is no way to prevent sniffing your actual code. Developing a proxy URL on your own site will prevent them to access your API key, but you may need a large bandwidth.
if you use routing in MVC or web api you can avoid this kind of redirecting
Ok I know this is a probably a very basic question but I'm more of a winform person.
Question is simple.
If we Add System.IO to a Web Form . Can we then use a StreamWriter to write a log file to the end user Computer?
For example if page_Load use the following code:
StreamWriter sr=New StreamWriter("C:\abc.log)
sr.Write("ABC")
Then where this abc.log file is created? on webserver or on end user? If on websever then how can we write it on the end user machine?
I want to write a log file on the client machine. What are options? Is using a cookie an option?
No - that is not the way the web works...
You could present the Log as an download, that would be the cleanest solution.
As everyone in this thread has pointed out: this is simply not possible. Now depending on what you are trying to store you could leverage cookies to store some information on the users computer but this can be cumbersome and you are limited to 4K of information. That being said HTML5 offers the new JavaScript objects localStorage and sessionStorage which are basically a key/value dictionaries. localStorage would probably suit your needs best I think since it will persist information on the users PC after the browser is closed. For example:
function logSomeData(message) {
localStorage.logFile += message;
}
function showData() {
alert(localStorage.logFile);
}
If you run the logSomeData function in a browser, close the browser, then come back and run the showData() function the value you added to logFile will be persisted. This isn't a new concept, this is doing exactly what cookies do today except that localStorage is easier to work with (in my opinion) and it can store much more information (2MB to 10MB depending on the browser). It is worth pointing out that this is a new technology so older browsers like IE7 can't use localStorage.
Please let me know if you have any other questions!
As far my knowledge you can't write the client file using asp.net.
In your example you are writing the file where application is hosted.
Means the application server's c:\abc.log
All the code behind in asp.net runs on the server so the code you posted will write to the server machine.
Browsers don't allow interaction with the local machine file system for security reasons
You could use some other technology (e.g. an ActiveX control hosted in a web page)
why you want log file on client site ?
if you want to store some information on client site use cookies
If you're stuck to Internet Explorer 6, the only solution to be able to store client-side data, is to use userData Behavior. It well let you store up to 128kb.
I would recommend you to have a look at jStorage. Which is:
a cross-browser key-value store database to store data locally in the
browser - jStorage supports all major browsers, both in desktop (yes -
even Internet Explorer 6) and in mobile.
It's mainly based on HTML5 localstorage but it will switch to any available technology (like userData Behavior) when HTML5 is not supported.
You could download the text file of log to client machine if you flush the .txt file in the respose object i,e (response.write)
we want to make a file cache for local internet provider (like squid).
Is it possible in C# to catch request for a file from user and send him local file instead of downloading it from remote server?
Thanks a lot!
Um, it depends upon what you mean by 'catch a request'. You're not talking about squid, are you? If so, problem solved :-).
Otherwise, if you are talking about catching requests that go through a specific web page, you could do it, but it would be pretty hairy. The idea would be to write an http handler that parses a webpage before it is served looking for links. If it finds one and has a local cache, you would re-write the link to point to your copy.
The other (um, better) alternative would be to parse for links when the pages are being created, which would save you a lot of processing power as the pages are being served. each link could be rewritten to pass through a proxy process that caches files and routes based upon its existence.
I told you it was hairy :-)
I need to let a company push information up to my site.
The best way to explain what I am talking about is to explain how it is currently done with their previous website:
This company uploads a CSV file to an FTP set up by the website. The website then processes the CSV file and puts it into an SQL database so that it can be used by the website.
In this case, I am the website and I am working with the company. Both sides are willing to change what they do. So my question is...
What is the best way to accept batch information like this? Is there a more automated way that doesn't involve FTP? In the future I may have a lot of companies wanting to do this, and I'd hate to have to setup accounts for each one.
The project is C# ASP.NET MSSQL
Let me know if you need more information...
Set up a web service to accept incoming data. That way you can validate immediately and reject bad data before it ever gets into your system.
If you want to eliminate FTP, you could allow them to upload files to your site leveraging using FileUpload. Once the file is uploaded you can do your server side processing.
EDIT: From the OP's comment's it seems to be an automated process. That said, if their process generates the file, you could:
Allow them to continue their current process which would involve them generating their file and placing it somewhere where it could be accessed via a URI with authentication, you could access this file on a schedule and process it. From what it seems right now they generate a file and upload it to your FTP server, so there seems to a manual element to begin with.