I'm working on a server/client application, in which the client has the ability to upload/download a file, which will be stored in SQL server (2012) as varbinay(max). The problem is that I want that the file will be uploaded directly to the database without saving the file on the server's hard drive, using ReadAllBytes method, which only accepts a path parameter.
Here is a fragment of the code used in the server side:
HttpPostedFile file1 = context.Request.Files[0];
byte[] buffer = new byte[file1.ContentLength];
file1.InputStream.Read(buffer, 0, file1.ContentLength);
Here is the code user in order to write into a file from data in the database:
foreach (var file in list)
{
System.IO.File.WriteAllBytes(path + file.FileName, file.FileContents);
}
The HttpPostedFile comes with an input stream you can read from. Read it into a byte array, then store it in the database. Or if your database interface (in the case of EF this is unlikely) directly accepts a stream, just put it in as-is,
Do note that ASP.NET (or was it IIS?) may store a file upload temporarily on disk anyways, if it's very large.
Related
I have a unique scenario in which I'd like the end result to help me upload a zip file. Here is what is happening in my workflow:
Our user is given an application on their local machine. With a click of a button, it will copy files and a zip file to remote-machine-1.
On remote-machine-2, it is running a .NET Core web app.
On remote-machine-1, I'd like to ping an endpoint off the web app in order to upload the zip file to remote-machine-2. However, the caveat is that the user will not be able to specify where this zip file is - the location of the zip file already known due to the structure of how the files and zip file are copied over in the first place.
So the question remains, with the code below - how do I pass in an IFormFile object when I call the endpoint localhost:5000/PublishTargetAsync?file=[???]? Or is there another workaround?
public async Task<bool> PublishTargetAsync(IFormFile file)
{
if (file != null)
{
using (var fileStream = new FileStream(Path.Combine(_targetOutputDirectory.ToFileSystemPath(), file.Name), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
return true;
}
A simple, but non optimized approach would be to use HttpClient and post the file contents as a base64 encoded string as Json using sample code similar to what is in my link. From there you could work your way back to using HttpWebRequests and a network stream and crafting the Http request by hand if necessary for performance, but the above approach should work for most small files. You'll have to modify your PublishTargetAsync endpoint to handle a post request with the right type.
When opening a file in new browser window that is stored in the file system with the url stored in the database, the solution is simple with the javascript method:
[var windowObjectReference = window.open(strUrl, strWindowName\[, strWindowFeatures\]);][1]
However, when storing files as blob in the database (SQL Server) there is no url pointing to the file. What is the best practice for retrieving the file for the user? Do I need to copy the file to a temp folder and then provide the url dynamically? Then delete the file after a certain point?
I know that storing the file in the file system would be easier to code, but I have chosen to store it as a blob for all the benefits that come with a DBMS.
Thanks for your help
Your web server will have to write the BLOB's bytes to a response stream, plus an appropriate Content-Type and Content-Length header so that the browser knows how to treat those bytes. Use a tool like Fiddler and browse to google.com. You can see what Google's responses contains for images on the page. Your goal is to generate a response in a similar fashion.
Trying to upload a data byte[] to ASP.NET via "WebClient" and OpenWriteCompleted in Silverlight5. If the data size is smaller all the data gets written correctly. If I upload say a 500kb file I get data corruption.
How do I upload a 500kb file to ASP.NET ??
NOTE: I'm trying to upload a zip file into a MSSQL varbinary(MAX) column in my database.
Never mind, it was a zip compression bug on my part.
I have a WebMethod which I use to allow a user to upload a database file (*.DBF). I know I can access the *.DBF file using a OleDbConnection with the appropriate connection string. But I dont really want to save the file on the server, my WebMethod has the file contents in a stream. Is there anyway I can access the *.DBF file while it is in the stream ?
Am using asp.net's FileUpload control to upload a word file to the server.
I want to encrypt the contents of this file (using our custom encryption API) and then save it in the server.
How do i achieve this?what should be my approach?
Thanks.
Whatever you do on the server side, do realize that while the data is transfering over the wire, it will not be encrypted with your custom api. You will need to upload the file using an ssl connection in order ensure the data transfer is secure.
if you have your own custom encryption API, you should use it to encrypt in a temporary file and then upload the temporary encrypted result, and delete it after upload complete.
The FileUpload control gives you a handle to the file. Read the data from the file stream, encrypt it and write it to a FileStream. If the encryption process is CPU intensive and/or takes a lot of time, save the file in a temp directory and start a new thread that reads the file, encrypts its contents, saves it to a new file and deletes the temp file.
maybe u should write your own encryptor as an app and then decrypt that file on the server
this article could help u : file encrypt / decrypt