关于文件上传下载的问题,详见内容,分不够再加!

qinamao 2003-11-06 05:28:13
1、上传文件(含路径文件全名变量:upfile)时,我想将upfile.ContentType存入sql数据库,但是好象编码问题,如upfile.ContentType="application/msword"

其中有"/",如何解决这个问题?
2、文件下载时,保存下来的是下载页面而不是要下载的文件,而且,对于中文,直接就不能显示中文名称下载。
try
{

strFileName = 上传时存入数据库的含绝对路径的文件名
strFileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(strFileName));
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename="+Server.UrlEncode(strFileName));
System.IO.FileStream MyFileStream =new FileStream(strFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);
long FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
Response.BinaryWrite(Buffer);



}
catch(Exception exp)
{
Response.Write("导出数据错误!");
}
...全文
86 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjmfdm 2003-11-08
  • 打赏
  • 举报
回复
不能保存为中文名
lfkool 2003-11-07
  • 打赏
  • 举报
回复
up
srj911 2003-11-07
  • 打赏
  • 举报
回复
关注
Kanderliu 2003-11-07
  • 打赏
  • 举报
回复
to 楼主:
第一个问题:你可以只存储msword,因为ContentType都包含application的.

第二个问题:你的strFileName是不能包含绝对路径的,只能是一个文件的全名(包括后缀)

详细情况你可以看我的帖子!
http://expert.csdn.net/Expert/topic/2397/2397957.xml?temp=.7909967
http://expert.csdn.net/Expert/topic/2391/2391961.xml?temp=.7228205
http://expert.csdn.net/Expert/topic/2405/2405474.xml?temp=.7963068
http://expert.csdn.net/Expert/topic/2410/2410608.xml?temp=8.951968E-02
acewang 2003-11-07
  • 打赏
  • 举报
回复
用aspx读取文件输出它
string strFile = "e:\\test.doc"; /////你的文件路径
FileStream fs = new FileStream(strFile, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(File.Name)) ////中文名
Response.BinaryWrite(bytes);
Response.End();
或:
Response.AppendHeader("Content-Disposition","attachment;
filename="+HttpUtility.UrlEncode(sFileName,System.Text.Encoding.UTF8));//sFileName文件名
Response.WriteFile(filePath);//filePath文件路径
Response.End();
goody9807 2003-11-07
  • 打赏
  • 举报
回复


<script language="VB" runat="server">
Sub UploadFile(sender As Object, e As EventArgs)

If FileUp.PostedFile.ContentLength = 0 Then
FileInfo.Visible = False
Exit Sub
Else
FileInfo.Visible = True
FDisplay1.Visible = True
End If

FSize.Text ="上传文件大小"+ CStr(FileUp.PostedFile.ContentLength/1024)+"KB"
FName.Text = "已上传文件名:"+FileUp.PostedFile.FileName+"<br>"+FName.Text

'写入数据库
on error resume next
dim myconn as sqlconnection
dim mycomm as sqlcommand
dim sql as string
dim id as integer
Dim image,anewimage As System.Drawing.Image
dim width,height,newwidth,newheight as integer
Dim callb As System.Drawing.Image.GetThumbnailImageAbort
myConn=New sqlconnection(ConfigurationSettings.AppSettings("数据库"))
myconn.open()
sql="insert into picture (姓名,班级,介绍,属性) values ('"&request.cookies("dgxyl").values("dgxylname")&"','"&request.cookies("dgxyl").values("dgxylbj")&"','"&trim(request("TextBox1"))&"','"&request("r1")&"')"
Mycomm=New sqlcommand(sql,myconn)
mycomm.executenonquery()
myconn.close()

Dim myCommand As New SqlCommand("select top 1 id from picture order by id desc", myConn)
myCommand.Connection.Open()
Dim myReader As SqlDataReader=mycommand.executereader()
if myReader.Read() then
id=myReader("id")
end if
myconn.close()
'保存图片
FileUp.PostedFile.SaveAs( Server.MapPath("\classpic\")&cstr(id)&".jpg" )
'生成缩略图
image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg"))
width=image.Width
height=image.height
if width>height then
newwidth=250
newheight=image.height/image.Width*newwidth
else
newheight=250
newwidth=image.Width/image.height*newheight
end if
response.write("id="+cstr(id)+"width="+cstr(Width)+";height="+cstr(height)+" ")
response.write("newwidth="+cstr(newwidth)+";newheight="+cstr(newheight)+"<br>")

aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())
aNewImage.Save(Server.MapPath("/smallpic/"+cstr(id)+".jpg"))
image.Dispose()


Dim FileSplit() As String = Split( FileUp.PostedFile.FileName, "\" )
Dim FileName As String = FileSplit(FileSplit.Length-1)
Dim Exts() As String = Split( FileName, "." )
Dim Ext As String = LCase(Exts(Exts.Length-1))

FDisplay.Text = "<A Target='_blank' HREF='/classpic/"&cstr(id)&".jpg"& "'>查看上传文件</A>"
FDisplay1.text="<a href='/picture/default.asp?bj="&cstr(request.cookies("dgxyl").values("dgxylbj"))&"'>返回</a>"
End Sub
</script>
<SCRIPT language=JavaScript>
<!--
var requestsubmitted=false;
function guestbook_Validator(theForm)
{
//检查是否从新提交
if (requestsubmitted==true){
alert("你已经提交了留言,请等待服务器应答!");
return(false);
}
requestsubmitted=true;

return (true);
}
//-->
</SCRIPT>
<Html>
<Body BgColor=White>
<H3 align="center">请正确填写下面各项</h3>
<Hr></H3>
<Div id="FileInfo" Visible="False" runat="server">
<Asp:Label id="FSize" runat="server"/><br>
<Asp:Label id="FName" runat="server"/><br>
<Asp:Label id="FDisplay" runat="server"/>
<Asp:Label id="FDisplay1" runat="server"/>
</Div>
<Form Enctype="multipart/form-data" onsubmit="return guestbook_Validator(this)" runat="server">
上传文件
<Input Type="File" id="FileUp" runat="server" size="20"><br>
图片属性:<input type="radio" value="<%=request.cookies("dgxyl").values("dgxylbj")%>" name="R1" checked>本班<input type="radio" value="全校" name="R1">全校(本班则只在本班显示,全校则在全校显示)<P>
图片说明:<br>
<asp:TextBox id="TextBox1" runat="server" Width="233px" Height="141px">
</asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="请输入图片说明">
</asp:RequiredFieldValidator><br>
<Asp:button id="Upload" OnClick="UploadFile" Text="上传图片" runat="server"/>
</form>
<Hr>
<p>注意:</p>
<ol>
<li><b>严禁上传污染环境的照片,否则账号将被删除!</b></li>
<li><font color="#808000"><b>请详细填写照片说明,图片说明不详细将会被视为乱传图片,将会被删除!</b></font></li>
</ol>
<p> </p>

</Body>
</Html>

qinamao 2003-11-07
  • 打赏
  • 举报
回复
to:gOODiDEA(无语)

我是将上传文件的存放路径存于数据库内,文件并不存入数据库,在下载文件时需要提供文件类型以作另存用。
gOODiDEA 2003-11-07
  • 打赏
  • 举报
回复
1、不需要指定ContentType,在库里都是以二进制存储的

2、参考:

System.IO.FileStream fs = System.IO.File.OpenRead( _FilePath );
byte[] FileData = new byte[ fs.Length ];
fs.Read( FileData, 0, ( int ) fs.Length );
Response.Clear();
Response.AddHeader( "Content-Type", "application/zip" );
string FileName = System.Web.HttpUtility.UrlEncode( System.Text.Encoding.UTF8.GetBytes( _FileName ) );
Response.AddHeader("Content-Disposition", "inline;filename="+ System.Convert.ToChar(34) + FileName + System.Convert.ToChar(34) );
Response.AddHeader("Content-Length", fs.Length.ToString() );
Response.BinaryWrite( FileData );
fs.Close();
System.IO.File.Delete( _FilePath );
rgbcn 2003-11-06
  • 打赏
  • 举报
回复
2
Response.AppendHeader("Content-Disposition", "attachment; filename="+Server.UrlEncode(strFileName));

不要用绝对路径。含盘符好像有这个题,你试一下
strFileName="test.doc" 传进去。应该是可以的。


userspring 2003-11-06
  • 打赏
  • 举报
回复
up
qinamao 2003-11-06
  • 打赏
  • 举报
回复
感谢acewang(**^o^**)
但是,我的做法如何解决?错在何处?
acewang 2003-11-06
  • 打赏
  • 举报
回复
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧