Click here to Skip to main content
15,896,338 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys, I have this code:

XML
<div>
            <ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError" OnClientUploadComplete="uploadComplete" runat="server"
                ID="AsyncFileUpload1" UploaderStyle="Traditional" CompleteBackColor="White" UploadingBackColor="#CCFFFF"
                ThrobberID="imgLoader" OnUploadedComplete="FileUploadComplete" />
            <%--<asp:Image ID="imgLoader" runat="server" ImageUrl="~/images/loader.gif" />--%>
            <asp:Label ID="lblMesg" runat="server" Text=""></asp:Label>
            <asp:Button ID="submit" runat="server" Text="SUBMIT REQUEST" OnClick="submit_Click" OnClientClick="submit_Click" />
        </div>


and on the background this:

C#
protected void FileUploadComplete(object sender, EventArgs e)
       {
           string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
           AsyncFileUpload1.SaveAs(Server.MapPath("Uploads/") + filename);
       }


Supposedly the file will be stuff like images, pictures, documents, etc...
but what I want to know is how I get the file in it's entirety? It's a long form and I already have a connection to a DB working so what I want is to get a variable that has the file saved to it so I can just pass it to the DB
Posted
Updated 26-Jan-15 0:00am
v2

file upload control has FileStream or some such property of type stream.

Put it in memoryStream class and convert to bytearray which can then be saved to binary column type.

Found it:

AsyncFileUpload1.PostedFile.InputStream

Here is full code for getting ByteArray:
C#
public static byte[] ReadFile(Stream input)
{
    byte[] buffer = new byte[input.Length];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}
 
Share this answer
 
Comments
varmartins 26-Jan-15 7:01am    
<pre>
protected void FileUploadComplete(object sender, EventArgs e)
{
string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
AsyncFileUpload1.SaveAs(Server.MapPath("Uploads/") + filename);
byte[] file = new byte[AsyncFileUpload1.PostedFile.InputStream.Length];
file = ReadFile(AsyncFileUpload1.PostedFile.InputStream);
Console.WriteLine(file);

}

public static byte[] ReadFile(Stream input)
{
byte[] buffer = new byte[input.Length];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
</pre>
I used this code but I can't use the file variable in the method I want which is in the same page, any idea why is that?
Sinisa Hajnal 26-Jan-15 7:18am    
because it is local to FileUploadComplete? You have to save it into the database in the same event - just call the method for that.

If you have some other mechanism then you have to declare byte[] file as the member variable of the class; or put it in a Session.
varmartins 26-Jan-15 7:34am    
Thanks m8, you really helped me ;)
varmartins 26-Jan-15 7:43am    
Btw you said to save to binary column type? but I'm getting this error "String or binary data would be truncated." and from what I saw this means it's 2 much data for the field but my field already has the maximum size allowd (8000) and it still doesn't fit... should I use a different type of field?
Sinisa Hajnal 26-Jan-15 8:02am    
If you have 8000 as size then yes, that is the maximum size. However there is varchar(MAX), varbinary (MAX) which hold 2GB and varbinary(max) FILESTREAM which is limited only to your filesystem max size.

You have to have SQL Server 2008 and above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900