Im making this website,where the user has a form in which they can choose a file from their computer,and upload it, but i don't know how i can use it. I'm using MVC and a Web Service also in C# where im handling the connection to the Database,where I have to save the file.The file can be a pdf,word or an image.
So the question is,how can I save it and also check its size.Thank you
You can use the ContentLength property of HttpPostedFileBase to get the size of the file -
https://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.contentlength(v=vs.110).aspx
Then save the InputStream to a byte array
using (MemoryStream stream = new MemoryStream())
{
file.InputStream.CopyTo(stream);
bytes = stream.ToArray();
}
Next assign the byte array to a SqlDbType.VarBinary stored procedure parameter to save it back to the database - assuming this is SQL Server.
Related
I am trying to read the file content from FileUpload control the following way
BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] binData = b.ReadBytes(FileUpload1.PostedFile.ContentLength);
I do not want to save the file to server, instead I wanted to get Byte array and
store it in database.
By doing the above it not only reads the contents of the file, but it also appends the html of the posted page. How do I get only the contents from the file.
Did you try to use FileBytes property?
I want to store files in my SQL Server database by C# which I have done it without problem.
This is my code:
byte[] file;
using (var stream = new FileStream(letter.FilePath, FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream))
{
file = reader.ReadBytes((int)stream.Length);
letter.ltr_Image = file;
}
}
LetterDB letterDB = new LetterDB();
id = letterDB.LetterActions(letter);
The insert SQL action in the LetterActions module. But I want to know, in order to reduce the size of the database (which increases daily) is there any solution for compressing the files and then store them in the database?
Yes , you can zip your files before storing them in the database, using the ZipFile class. Take a look here: https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx
Plenty of sample code out there too. See here:http://imar.spaanjaars.com/414/storing-uploaded-files-in-a-database-or-in-the-file-system-with-aspnet-20
You can compress file like this. Then insert compressed file stream into DB, but when you read it you need decompress it.
If you really need store file in DB, suggest you compress and decompress it by client.
And better way handle file is store them in disk, and only store file path in DB, when client need file use file path get file.
im new to c#, can somone please tell me how to add image to database, from picture box using c#.
i have a registration form which user is added to database(SQL 2008), but i have no idea how to add image of the user to database, which contain many columns of user information and a column of picture.
sql<2008> visual studio <2010>
You should have a binary field in DB for that. Read your image like binary array and save in into DB. But It is not goog practice as for me. I usually save image in cloud or folder and in DB - only URL
create table IMAGELOAD
(img1 binary)
while saving send path of the file to this field
Suggestion :
Instead saving image into database.Save in application folder.
Can you be more specific?
What you don't know how to do?
How to get image from pictureBox? This can help you, hope.
//Save content of imageBytes to db VARBINARY(MAX)
byte[] imageBytes;
using (imgStr = new System.IO.MemoryStream())
{
pictureBox.Image.Save(imgStr, System.Drawing.Imaging.ImageFormat.Jpeg); // Depending on your format.
imageBytes = imgStr.ToArray();
}
//to load from db use
using (Stream imgStr = new MemoryStream(imageBytes))
{
pictureBox.Image = System.Drawing.Image.FromStream(imgStr);
}
I am trying to store some pictures into my SQL Server database from a Silverlight project, and I need some help, so my questions are:
How to convert an image to binary from a url to store it into my database (store all the image and not only the url)
Are there any other solutions, without passing by binary type? (since it exist the image type in SQL Server)
Finally, when the image is stored, how to read it from Silverlight?
Thank you in advance .
You'll want to convert the System.Drawing.Image to a byte array and save the byte array to the database.
System.Drawing.Image image;
System.IO.MemoryStream imageStream;
byte[] imageBytes;
// image = your image object
imageStream = new System.IO.MemoryStream();
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Use whatever format your image is.
imageBytes = imageStream.ToArray();
// Save imageBytes to a DB column of type VARBINARY(MAX)
To get the data back into an System.Drawing.Image object from a byte array use System.Drawing.Image.FromStream(System.IO.Stream stream).
Download file contents and put into memory stream. See : http://www.csharp-examples.net/download-files/
Add bytes from memory stream into database
Get image with silverlight in C# by using the image class and method FromStream see http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx
http://www.pitorque.de/MisterGoodcat/post/Storing-images-in-SQL-Server-with-RIA-Services.aspx
It is fully functional as in the user can select images from the local disk and upload them to the service where they are stored in the database. The user can retrieve a list of all images in the database and download them to the client for watching, and they can delete existing images from the database.
im tasked to create a virtual database that clients can create from our web application and have them save data to.
the problem now is to have it stored for later re-use.
im thinking of dynamically creating a DataTable object in c# then convert it to byte[]. now i want to know if this would be practical to save on a database...
is this possible?
You can use WriteXml to write to a stream:
byte[] raw;
using(MemoryStream ms = new MemoryStream()) {
table.WriteXml(ms);
raw = ms.ToArray();
}
...
raw = ...
using(MemoryStream ms = new MemoryStream(raw)) {
table.ReadXml(ms);
}
And write the byte[] to a varbinary(max) / image / etc column in a database if that is what you need (you mentioned database) - or just use xml.
If you are writing to a file, then just use a FileStream in place of a MemoryStream.