文件上传保存文件夹而不是文件
Posted
技术标签:
【中文标题】文件上传保存文件夹而不是文件【英文标题】:File upload saving the folder instead of the file 【发布时间】:2022-01-15 02:32:57 【问题描述】:我正在使用 Telerik 的 ASP.NET RadAsyncpload 将文件上传到 DB,这与普通的 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();
【问题讨论】:
【参考方案1】:您似乎正在将文件数据读入名为 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();
【讨论】:
不幸的是,FileContent 被保存为空。所以肯定还缺什么 我知道我犯了一个小错误,声明fileStream.Read(dados, 0, System.Convert.ToInt32(fileStream.Length));
应该改为 fileStream.Read(fileBytes, 0, System.Convert.ToInt32(fileStream.Length));
是的,我注意到了。我也是我的小姐。但是问题仍然存在,您对它可能是什么有其他想法?【参考方案2】:
已解决!
我错过了将流复制到变量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())));
【讨论】:
以上是关于文件上传保存文件夹而不是文件的主要内容,如果未能解决你的问题,请参考以下文章