将文件上载到Web服务器并进行错误处理 [英] Upload a File to a Web Server and error handling

查看:68
本文介绍了将文件上载到Web服务器并进行错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来实现正确的错误处理。


我正在尝试根据下面的示例代码上传文件。代码工作

,除非所选文件太大。


我知道< httpRuntime>的maxRequestLength参数部分

并且按预期工作。我想要的是强制执行最大文件大小但是

无法捕获文件太大时抛出的错误和

那是'我可以使用一些帮助。


谢谢

主要代码如下所示,是从MS样本中复制的。

< script language =" VB" runat =" server">

Sub Button1_Click(Source As Object,e As EventArgs)


如果Text1.Value =""然后

Span1.InnerHtml ="错误:您必须输入文件名

返回

结束如果

如果不是(File1.PostedFile什么都没有)那么

尝试

File1.PostedFile.SaveAs((" c:\ temp \" & Text1.Value))

Span1.InnerHtml ="文件已成功上传至< b> c:\ temp \" &安培; _

Text1.Value& "< / B个在Web服务器上

Catch exc As Exception

Span1.InnerHtml ="保存文件时出错< b> c:\ temp \" &安培; _

Text1.Value& "< / B><峰; br>" &安培; exc.ToString()

结束尝试

结束如果

结束子''Button1_Click

< / script> ;


< form enctype =" multipart / form-data" runat =" server">


选择要上传的文件:

< input id =" File1"

type =" file"

maxlength =" 30"

runat =" server" />


< ; p />

另存为文件名(无路径):

< input id =" Text1"

type =" text"

runat =" server" />


< p />

< span id = " Span1"

style =" font:8pt verdana;"

runat =" server" />


< p />

< input type =" button"

id =" Button1"

value =" Upload"

OnServerClick =" Button1_Click"

runat =" server">


< / form>

I need help in implementing proper error handling.

I am trying to upload a file based on the sample code below. The code works
well except if the file selected is too big.

I do know about the maxRequestLength parameter of the <httpRuntime> section
and that works as expected. What I want is to enforce a max file size but
haven''t been able to trap the error thrown when the file is too large and
that''s where I could use some help.

Thanks
The main code is shown below and was copied from one of MS samples.
<script language="VB" runat="server">
Sub Button1_Click(Source As Object, e As EventArgs)

If Text1.Value = "" Then
Span1.InnerHtml = "Error: you must enter a file name"
Return
End If

If Not (File1.PostedFile Is Nothing) Then
Try
File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value))
Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _
Text1.Value & "</b> on the Web server"
Catch exc As Exception
Span1.InnerHtml = "Error saving file <b>c:\temp\" & _
Text1.Value & "</b><br>" & exc.ToString()
End Try
End If
End Sub ''Button1_Click
</script>

<form enctype="multipart/form-data" runat="server">

Select File to Upload:
<input id="File1"
type="file"
maxlength="30"
runat="server"/>

<p/>
Save as filename (no path):
<input id="Text1"
type="text"
runat="server"/>

<p/>
<span id="Span1"
style="font: 8pt verdana;"
runat="server" />

<p/>
<input type="button"
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">

</form>


推荐答案

Al Smith你可以试试这个 -


Dim strFolderDetail As String =" C:\\\ temppub \wwwroot \ MYPROJECTFOLDER \uploads \


''抓住来自客户端的完全限定路径的文件名

Dim strFileName As String = controlName.PostedFile.FileNam


''只有附加的文件名不是它的pat

Dim strNewFileName As String = System.IO.Path.GetFileNameWithoutExtension(strFile Name)&

String.Format(" {0:MMddyyHHmmss}",Date.Now )&

System.IO.Path.GetExtension(strFileName


''将上传的文件保存到C的服务器:\ ServerFolder Tr

controlname.PostedFile.SaveAs(strFolderDetail + strNewFileName

Response.Write("您的文件在服务器上成功上传为:" &安培; strNewFileName


Catch Exp As Exceptio

Response.Write(Exp.Message

结束Tr

-------------

你在aspx页面中的
你需要pu


< INPUT id = " controlName" type =" file" size =" 40" name =" controlName" runat =" server"

如果您在运行此问题时遇到任何问题,请与我们联系。

Joji
Al Smith you can try this -

Dim strFolderDetail As String = "C:\inetpub\wwwroot\MYPROJECTFOLDER\uploads\

''Grab the file name from its fully qualified path at client
Dim strFileName As String = controlName.PostedFile.FileNam

'' only the attched file name not its pat
Dim strNewFileName As String = System.IO.Path.GetFileNameWithoutExtension(strFile Name) &
String.Format("{0:MMddyyHHmmss}", Date.Now) &
System.IO.Path.GetExtension(strFileName

''Save uploaded file to server at C:\ServerFolder Tr
controlname.PostedFile.SaveAs(strFolderDetail + strNewFileName
Response.Write("Your File Uploaded Sucessfully at server as: " & strNewFileName

Catch Exp As Exceptio
Response.Write(Exp.Message
End Tr
-------------

in you aspx page you have to pu

<INPUT id="controlName" type="file" size="40" name="controlName" runat="server"
Let me know if you have any problem running this.
Joji


你好Joji,


我的问题是文件的字节数超过maxRequestLength

web.config文件的< httpRuntime>区域的参数以及如何捕获

该错误。所以该文件的名称不是问题... at至少,或者确实

我想念这里的东西。


谢谢

Al


" Joji" jo ******** @ yahoo.com>在留言中写道

news:23 ************* ********************* @ microsof t.com ...
Hi Joji,

My problem is when the byte count of the file exceeds the maxRequestLength
parameter of the <httpRuntime> area of the web.config file and how to trap
that error. So the name of the file is not an issue... at least yet, or did
I miss understand something here.

Thanks
Al

"Joji" <jo********@yahoo.com> wrote in message
news:23**********************************@microsof t.com...
Al Smith你可以试试这个 -

Dim strFolderDetail As String =
" C:\inetpub \wwwroot \ MYPROJECTFOLDER \uploads \"
''从客户端的完全限定路径中获取文件名
Dim strFileName As String = controlName.PostedFile.FileName

''只有附加文件名而不是其路径
Dim strNewFileName As String =
System.IO.Path.GetFileNameWithoutExtension(strFile Name)& _
String.Format(" {0:MMddyyHHmmss}",Date.Now)& _
System.IO.Path.GetExtension(strFileName)

''将上传的文件保存到服务器C:\ ServerFolder试试
controlname.PostedFile.SaveAs(strFolderDetail +
strNewFileName)Response.Write(你的文件在服务器上成功上传为:"
& strNewFileName)
Catch Exp as Exception
Response.Write(Exp.Message)<结束尝试
--------------

在你的aspx页面你必须把

< INPUT ID = QUOT;控件名称"类型= QUOT;文件"大小= QUOT; 40"名称= QUOT;控件名称"
runat =" server">如果你有任何问题,请告诉我。
Joji
Al Smith you can try this -

Dim strFolderDetail As String = "C:\inetpub\wwwroot\MYPROJECTFOLDER\uploads\"
''Grab the file name from its fully qualified path at client
Dim strFileName As String = controlName.PostedFile.FileName

'' only the attched file name not its path
Dim strNewFileName As String = System.IO.Path.GetFileNameWithoutExtension(strFile Name) & _ String.Format("{0:MMddyyHHmmss}", Date.Now) & _ System.IO.Path.GetExtension(strFileName)

''Save uploaded file to server at C:\ServerFolder Try
controlname.PostedFile.SaveAs(strFolderDetail + strNewFileName) Response.Write("Your File Uploaded Sucessfully at server as: " & strNewFileName)
Catch Exp As Exception
Response.Write(Exp.Message)
End Try
--------------

in you aspx page you have to put

<INPUT id="controlName" type="file" size="40" name="controlName" runat="server"> Let me know if you have any problem running this.
Joji



你好AI,


感谢您在社区发帖。关于这个问题,我是

找到合适的资源来帮助你,我们会尽快更新。


问候,


Steven Cheng

Microsoft在线支持


安全! www.microsoft.com/security(This )发布按原样提供;,

没有保证,也没有授予任何权利。)

Hi AI,

Thank you for posting in community. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)


这篇关于将文件上载到Web服务器并进行错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆