How to upload and download a file
I like to upload an file in my project. when I click the upload button the file should be stored in client system and the file name and path should be stored in the database. When I clicking the download button it should be downloaded based on the file name and path that I have stored in the database. After making the changes it should be uploaded as different file name and it will not affect the previous file content. If there is any code for this process please send开发者_如何学运维 it to me.
Thanks in advance
To upload a file you use the input
type file
and then process this accordingly on the server. Here is a complete tutorial on CodePlex that goes through exactly what you are looking for.
Warning don't use their code in production. Just noticed a couple of security risks, but anyways, use this to understand the process then figure out how to avoid sql-injections and possible overflows.
Here is another great article over at MSDN that covers File Uploading in ASP.NET 2.0.
string FolderPath = "yourpath";
string FileName = "Namefile";
string FilePath = Server.MapPath("~/" + FileName);
string Extension = Path.GetExtension(FileName);
Response.ContentType = "Application/x-msexcel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + "");
// Write the file to the Response
const int bufferLength = 10000;
byte[] buffer = new Byte[bufferLength];
int length = 0;
Stream download = null;
try {
download = new FileStream(Server.MapPath("~/" + FileName),
FileMode.Open,
FileAccess.Read);
do {
if (Response.IsClientConnected) {
length = download.Read(buffer, 0, bufferLength);
Response.OutputStream.Write(buffer, 0, length);
buffer = new Byte[bufferLength];
}
else {
length = -1;
}
}
while (length > 0);
Response.Flush();
Response.End();
}
finally {
if (download != null)
download.Close();
}
精彩评论