Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4webforms

Could not find file 'c:\windows\system32\inetsrv\download (2).jpg'. when publish on server


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;
        }


Solution

  • 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);