asp.net 无法找到文件“c:\windows\system32\inetsrv\download(% 2)”,jpg'.在服务器上发布时

vdgimpew  于 2023-05-02  发布在  .NET
关注(0)|答案(1)|浏览(152)

我有一个 www.example.com :无法找到文件“c:\windows\system32\inetsrv\download(% 2)”。jpg,你能告诉我我能做什么吗?谢谢。

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

brccelvz1#

问题出现在FileToByteArray函数中,因为您试图从文件系统中读取文件,而不是阅读HttpPostedFile,当然它不存在。(在你的开发机器上,你可能有一个文件保存用于测试目的,或者你试图从你的wwwroot路径上传文件!)
试试这个代码:

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

并将UploadFileToDatabase中的调用更新为:

byte[] ByteFile = FileToByteArray(file);

相关问题