-1

I have an asp.net web forms web application and I save uploaded images with binary formats in database and then for loading the images I convert the binary data to base64 string and load it,in my local server when I run visual studio it works fine with not any problem,but unfortuantly when I publish it to a server then after choose file it can't upload the image and it gets this exception error : Could not find file 'c:\windows\system32\inetsrv\download (2).jpg'.,could you please tell me what can I do? thanks a lot.

var ImageCode = tools.UploadFileToDatabase(file, "اخبار");
 public int UploadFileToDatabase(HttpPostedFile file,string FileSubject)
    {
        var extension = GetFileExtension(file.FileName);
        byte[] ByteFile = FileToByteArray(file.FileName);
        BOLAmlakFiles bOLAmlakFiles = new BOLAmlakFiles();
        var resultCode = bOLAmlakFiles.InsertFile(file.FileName, ByteFile, FileSubject,
                    extension);
        return resultCode;
    }
 public byte[] FileToByteArray(string fileName)
    {
        byte[] fileContent = null;
        System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
        long byteLength = new System.IO.FileInfo(fileName).Length;
        fileContent = binaryReader.ReadBytes((Int32)byteLength);
        fs.Close();
        fs.Dispose();
        binaryReader.Close();
        return fileContent;
    }
public int InsertFile(string name, byte[] content, string category,
        string FileExtension)
    {

        try
        {
            Files ObjTable = new Files();
            dataContext.Files.InsertOnSubmit(ObjTable);

            ObjTable.Name = name;
            ObjTable.Content = content;
            ObjTable.Category = category;
            ObjTable.CreationDate = DateTime.Now;
            ObjTable.FileExtencion = FileExtension;

            dataContext.SubmitChanges();
            dataContext.Connection.Close();
            return ObjTable.Code;

        }
        catch (Exception)
        {
            return 0;
        }

  • Use `Server.MapPath()` to construct the full path to your files relative to where your application is located, rather than the current directory of IIS itself: https://stackoverflow.com/a/11105933/1064169 – Martin Costello Apr 30 '23 at 08:40
  • Hello,Thanks for your response,but I don't save the images in a folder with specific path,as you see in the code first the images convert to byteArray and binary format then save in database directly,then I load them by converting them to base64string – PEDRAM POURHAKIM May 01 '23 at 05:32

1 Answers1

1

The problem arises in your FileToByteArray function because, instead of reading the HttpPostedFile , you try to read the file from the file system and of course it does not exist. (on your dev machine you probably have a file saved for test purposes or you try to upload the file from your wwwroot path!)

Try this code:

 public byte[] FileToByteArray(HttpPostedFile file)
    {
        byte[] fileContent = new byte[file.ContentLength];

        System.IO.Stream MyStream = file.InputStream;            
        MyStream.Read(fileContent, 0, file.ContentLength);

        return fileContent;
    }

and update the call in UploadFileToDatabase to:

byte[] ByteFile = FileToByteArray(file);
Nicola P
  • 81
  • 3