사용자가 다양한 파일 (HttpPostedFile)을 업로드 할 수 있도록 웹 사이트를 만들려고합니다. 그런 다음 Oracle 데이터베이스에 BLOB로 저장됩니다. 여기에 내가 지금까지 가지고있는 것이있다 :Oracle DB에 업로드하기 전후에 파일의 불일치가 발생했습니다.
public static bool insertFile(int pid, HttpPostedFile file, string filedesc)
{
string filename = file.FileName.Remove(0, file.FileName.LastIndexOf("\\") + 1);
byte[] filebytearray = new byte[file.ContentLength];
BinaryReader br = new BinaryReader(file.InputStream);
filebytearray = br.ReadBytes(file.ContentLength);
if (filedesc == string.Empty)
{
filedesc = "No description.";
}
OracleConnection conn = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand("database", conn);
cmd.BindByName = true;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("pfiledata", OracleDbType.Blob)).Value = filebytearray;
try
{
conn.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Errors.WriteToEventLog("File insert", ex);
return false;
}
finally
{
conn.Dispose();
cmd.Dispose();
file.InputStream.Dispose();
}
}
파일이 성공적으로 업로드되고 다운로드되지만 다운로드 한 파일은 업로드 된 파일과 동일하지 않다. 나는 내용이 데이터베이스에 들어갔다 나오는 파일과 동일하다는 것을 이미 확인했다. 즉, 파일이 올바르게 변환되지 않았거나 클라이언트에 의해 올바르게 저장되지 않았다는 것을 의미한다. 두 파일의 크기는 디스크에서는 동일하지만 Windows에서는 동일하지 않습니다. 16 진수 편집기에 따르면 파일의 다운로드 된 사본에는 파일의 맨 처음에 3 바이트가 누락 된 것처럼 보입니다.
다음은 클라이언트에 파일을 전송할 때 사용하는 것입니다. Response.Clear(); Response.AddHeader ("Content-Disposition", "attachment; filename ="+ fileinfo [1]);
Response.AddHeader ("Content-Length", filedata.Length.ToString());
Response.ContentType = "application/octet-stream"; Response.BinaryWrite (filedata);
도움을 주시면 감사하겠습니다.
감사합니다. BinaryReader가 인덱스 0을 기본값으로 사용하여 스트림을 캡처하지 않는다는 것이 다소 이상한 것처럼 보입니다.하지만 이제는 제대로 작동하지 않을 것입니다. 너무 많이 질문하지 않을 것입니다. –