Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used this code to upload the file to my page
C#
protected void upload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string fileName = FileUpload2.FileName;
                string path = Path.GetFullPath(FileUpload2.FileName);
                TextBox2.Text = fileName;
                FileUpload2.SaveAs(Server.MapPath(fileName));
                passport1img.ImageUrl = path;
            }
        }


but i receive this error when i click to upload to the database

(Message)MySqlException: Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\mfb1.jpg'.
(Source)MySqlException: mscorlib
(StackTrace)MySqlException at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at SchoolManagementSystem.schlreg.processreg_Click(Object sender, EventArgs e) in C:\Users\israel asha\documents\visual studio 2010\Projects\SchoolManagementSystem\SchoolManagementSystem\schlreg.aspx.cs:line 113

and i used this to convert it into binary format for upload into the database

byte[] passport2 = null;
FileStream fs2 = new FileStream(TextBox2.Text, FileMode.Open, FileAccess.Read);
BinaryReader br2 = new BinaryReader(fs2);
passport2 = br2.ReadBytes((int)fs2.Length); // int here is to cast d image into whole numbers

line 113 being this line
ASM
byte[] passport2 = null;
                FileStream fs2 = new FileStream(TextBox2.Text, FileMode.Open, FileAccess.Read);


Thanks for your usual assistance
Posted

The error that you've shown us is enough to elaborate the problem that the file you're trying to pass to the FileStream is not found.

Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\mfb1.jpg'.


It shows, that the file was not found at that location. Make sure the file exists, I believe it is some sort of dependency file (but a .jpg file? Microsoft, you sure?).
 
Share this answer
 
The FileName property returns either the name of the file, or the path of the file on the client, depending on the browser settings. In this case, it looks like you're getting just the name.

Path.GetFullPath will resolve the path of the file relative to the current path. In this case, the current path is the folder containing the Visual Studio development server.

Server.MapPath will resolve the path relative to the currently executing page. This is not necessarily the same as the current path used by GetFullPath.

You need to save the file in a known location, to which your site must have write access. For example, if you want to save the file in a folder called images in the root of your site:
C#
protected void upload_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        string fileName = Path.GetFileName(FileUpload1.FileName);
        string virtualPath = "~/images/" + fileName;
        string physicalPath = Server.MapPath(virtualPath);
        FileUpload1.SaveAs(physicalPath);
        passport1img.ImageUrl = virtualPath;
        
        SaveToDatabase(physicalPath);
    }
}

private void SaveToDatabase(string pathToImageFile)
{
    byte[] fileBytes = File.ReadAllBytes(pathToImageFile);
    ...
}

Alternatively, you could skip saving the file to disk, and save it directly to the database:
C#
protected void upload_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        int length = (int)FileUpload1.FileContent.Length;
        byte[] fileBytes = new byte[length];
        FileUpload1.FileContent.Read(fileBytes, 0, length);
        
        SaveToDatabase(fileBytes);
    }
}

private void SaveToDatabase(byte[] fileBytes)
{
    ...
}
 
Share this answer
 
Comments
EasyHero 8-Oct-14 16:15pm    
still not getting d desired result. i changed my code for saving into the database into the code below, it didn't give me any error but it didnot upload a file to the database i.e. the size of file in my database is zero

byte[] passport1 = null;
Stream fs1 = FileUpload1.PostedFile.InputStream;
BinaryReader br1 = new BinaryReader(fs1);
passport1 = br1.ReadBytes((Int32)fs1.Length);
Richard Deeming 9-Oct-14 7:24am    
You'll need to debug your code to make sure the passport1 array contains the file bytes, and that those bytes are correctly passed to the database command parameter.

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