文件上传保存文件夹而不是文件
File upload saving the folder instead of the file
我正在使用 Telerik 的 ASP.NET RadAsyncpload 将文件上传到数据库,这与普通的 fileUpload 非常相似。
我的问题是,当使用变量存储数据时,它保存为“null”。或者我尝试使用 Server.MapPath,这是为了保存文件夹位置而不是文件。
我做错了什么?
//partial class declarations
(...)
string Ficheiro = string.Empty;
string FileTipo = string.Empty;
byte[] fileBytes = null;
//Save method, triggered by save button after upload
public void SaveFile(object sender, EventArgs e)
{
ListagemTimesheet model = new ListagemTimesheet();
model.IDRecursoHumano = Convert.ToInt32(rdpInvestigadorE.Text);
model.IDEstadoTimesheet = Convert.ToInt32(rcbEstado.SelectedValue);
model.Observações = Obervaçoestxt.Text;
model.AssinaturaTimesheet = txtAssinaturaTimesheet.Text;
model.DataEnvio = DataEnvio.SelectedDate.Value;
if (Objecto.ID > 0)
{
model.ID = Convert.ToInt32(FileID.Text);
if (!string.IsNullOrEmpty(Ficheiro) && FileTipo != null)
{
model.FileName = Path.GetFileNameWithoutExtension(Ficheiro); //FileName
model.FileTipo = Path.GetExtension(FileTipo); //FileExtension
model.FileContent = fileBytes; //Content null
model.FileContent = Encoding.ASCII.GetBytes(HttpContext.Current.Server.MapPath("~/TargetFiles/ + model.FileName")); //Content saved is location of the folder
//Upload method
public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
RadAsyncUpload1.Visible = false;
Stream fileStream = e.File.InputStream;
Ficheiro = e.File.FileName; // sintaxe metodo
FileTipo = e.File.ContentType;
e.IsValid = true;
byte[] fileBytes = new byte[fileStream.Length - 1 + 1];
fileStream.Read(dados, 0, System.Convert.ToInt32(fileStream.Length));
fileStream.Close();
}
您似乎正在将文件数据读入名为 dados
的局部变量,但从未分配给全局变量。
您的文件数据最有可能进入 fileBytes
变量,以使您的 SaveFile
函数能够读取该值。
所以你应该将 fileStream
直接读入你的 fileBytes
变量。
...
byte[] fileBytes = new byte[fileStream.Length - 1 + 1];
...
完整代码
//Save method, triggered by save button after upload
public void SaveFile(object sender, EventArgs e)
{
ListagemTimesheet model = new ListagemTimesheet();
model.IDRecursoHumano = Convert.ToInt32(rdpInvestigadorE.Text);
model.IDEstadoTimesheet = Convert.ToInt32(rcbEstado.SelectedValue);
model.Observações = Obervaçoestxt.Text;
model.AssinaturaTimesheet = txtAssinaturaTimesheet.Text;
model.DataEnvio = DataEnvio.SelectedDate.Value;
if (Objecto.ID > 0)
{
model.ID = Convert.ToInt32(FileID.Text);
if (!string.IsNullOrEmpty(Ficheiro) && FileTipo != null)
{
model.FileName = Path.GetFileNameWithoutExtension(Ficheiro); //FileName
model.FileTipo = Path.GetExtension(FileTipo); //FileExtension
model.FileContent = fileBytes; // This will now be the data loaded from the filestream in your RadAsyncUpload1_FileUploaded function
// This returns the path in the content because Encoding.ASCII.GetBytes simply changes the string path to a byte array
//model.FileContent = Encoding.ASCII.GetBytes(HttpContext.Current.Server.MapPath("~/TargetFiles/ + model.FileName")); //Content saved is location of the folder
//Upload method
public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
RadAsyncUpload1.Visible = false;
Stream fileStream = e.File.InputStream;
Ficheiro = e.File.FileName; // sintaxe metodo
FileTipo = e.File.ContentType;
e.IsValid = true;
// Read the file stream into your fileBytes variable
byte[] fileBytes = new byte[fileStream.Length - 1 + 1];
fileStream.Read(fileBytes, 0, System.Convert.ToInt32(fileStream.Length));
fileStream.Close();
}
已解决!
我缺少将流复制到变量 fileBytes
.
public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
RadAsyncUpload1.Visible = false; //false
var liItem = new HtmlGenericControl("li");
Ficheiro = e.File.FileName; // sintaxe metodo
FileTipo = e.File.ContentType;
e.IsValid = true;
e.File.InputStream.Position = 0;
fileBytes = new byte[e.File.InputStream.Length];
for (int totalBytesCopied = 0; totalBytesCopied < e.File.InputStream.Length; )
totalBytesCopied += e.File.InputStream.Read(fileBytes, totalBytesCopied, Convert.ToInt32(e.File.InputStream.Length) - totalBytesCopied); //conversao para bytes
e.File.InputStream.Close();
if (File.Exists("~/App_Data/RadUploadTemp"))
{
e.IsValid = false;
e.File.SaveAs(Server.MapPath(
Path.Combine(RadAsyncUpload1.TargetFolder, e.File.GetNameWithoutExtension() + "1" + e.File.GetExtension())));
}
}
我正在使用 Telerik 的 ASP.NET RadAsyncpload 将文件上传到数据库,这与普通的 fileUpload 非常相似。
我的问题是,当使用变量存储数据时,它保存为“null”。或者我尝试使用 Server.MapPath,这是为了保存文件夹位置而不是文件。
我做错了什么?
//partial class declarations
(...)
string Ficheiro = string.Empty;
string FileTipo = string.Empty;
byte[] fileBytes = null;
//Save method, triggered by save button after upload
public void SaveFile(object sender, EventArgs e)
{
ListagemTimesheet model = new ListagemTimesheet();
model.IDRecursoHumano = Convert.ToInt32(rdpInvestigadorE.Text);
model.IDEstadoTimesheet = Convert.ToInt32(rcbEstado.SelectedValue);
model.Observações = Obervaçoestxt.Text;
model.AssinaturaTimesheet = txtAssinaturaTimesheet.Text;
model.DataEnvio = DataEnvio.SelectedDate.Value;
if (Objecto.ID > 0)
{
model.ID = Convert.ToInt32(FileID.Text);
if (!string.IsNullOrEmpty(Ficheiro) && FileTipo != null)
{
model.FileName = Path.GetFileNameWithoutExtension(Ficheiro); //FileName
model.FileTipo = Path.GetExtension(FileTipo); //FileExtension
model.FileContent = fileBytes; //Content null
model.FileContent = Encoding.ASCII.GetBytes(HttpContext.Current.Server.MapPath("~/TargetFiles/ + model.FileName")); //Content saved is location of the folder
//Upload method
public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
RadAsyncUpload1.Visible = false;
Stream fileStream = e.File.InputStream;
Ficheiro = e.File.FileName; // sintaxe metodo
FileTipo = e.File.ContentType;
e.IsValid = true;
byte[] fileBytes = new byte[fileStream.Length - 1 + 1];
fileStream.Read(dados, 0, System.Convert.ToInt32(fileStream.Length));
fileStream.Close();
}
您似乎正在将文件数据读入名为 dados
的局部变量,但从未分配给全局变量。
您的文件数据最有可能进入 fileBytes
变量,以使您的 SaveFile
函数能够读取该值。
所以你应该将 fileStream
直接读入你的 fileBytes
变量。
...
byte[] fileBytes = new byte[fileStream.Length - 1 + 1];
...
完整代码
//Save method, triggered by save button after upload
public void SaveFile(object sender, EventArgs e)
{
ListagemTimesheet model = new ListagemTimesheet();
model.IDRecursoHumano = Convert.ToInt32(rdpInvestigadorE.Text);
model.IDEstadoTimesheet = Convert.ToInt32(rcbEstado.SelectedValue);
model.Observações = Obervaçoestxt.Text;
model.AssinaturaTimesheet = txtAssinaturaTimesheet.Text;
model.DataEnvio = DataEnvio.SelectedDate.Value;
if (Objecto.ID > 0)
{
model.ID = Convert.ToInt32(FileID.Text);
if (!string.IsNullOrEmpty(Ficheiro) && FileTipo != null)
{
model.FileName = Path.GetFileNameWithoutExtension(Ficheiro); //FileName
model.FileTipo = Path.GetExtension(FileTipo); //FileExtension
model.FileContent = fileBytes; // This will now be the data loaded from the filestream in your RadAsyncUpload1_FileUploaded function
// This returns the path in the content because Encoding.ASCII.GetBytes simply changes the string path to a byte array
//model.FileContent = Encoding.ASCII.GetBytes(HttpContext.Current.Server.MapPath("~/TargetFiles/ + model.FileName")); //Content saved is location of the folder
//Upload method
public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
RadAsyncUpload1.Visible = false;
Stream fileStream = e.File.InputStream;
Ficheiro = e.File.FileName; // sintaxe metodo
FileTipo = e.File.ContentType;
e.IsValid = true;
// Read the file stream into your fileBytes variable
byte[] fileBytes = new byte[fileStream.Length - 1 + 1];
fileStream.Read(fileBytes, 0, System.Convert.ToInt32(fileStream.Length));
fileStream.Close();
}
已解决!
我缺少将流复制到变量 fileBytes
.
public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
RadAsyncUpload1.Visible = false; //false
var liItem = new HtmlGenericControl("li");
Ficheiro = e.File.FileName; // sintaxe metodo
FileTipo = e.File.ContentType;
e.IsValid = true;
e.File.InputStream.Position = 0;
fileBytes = new byte[e.File.InputStream.Length];
for (int totalBytesCopied = 0; totalBytesCopied < e.File.InputStream.Length; )
totalBytesCopied += e.File.InputStream.Read(fileBytes, totalBytesCopied, Convert.ToInt32(e.File.InputStream.Length) - totalBytesCopied); //conversao para bytes
e.File.InputStream.Close();
if (File.Exists("~/App_Data/RadUploadTemp"))
{
e.IsValid = false;
e.File.SaveAs(Server.MapPath(
Path.Combine(RadAsyncUpload1.TargetFolder, e.File.GetNameWithoutExtension() + "1" + e.File.GetExtension())));
}
}