上传文件 [英] Uploadfile
本文介绍了上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi ,,
我需要使用上传文件上传图片,我编写了类似FileUpload1.PostedFile.ContentType的代码=" image/.jpg
但它不起作用,如何编写代码请告诉我
在此先感谢
,
i need to upload image by using upload file,i wrote code like FileUpload1.PostedFile.ContentType == "image/.jpg
but its not working,how to write the code please tell me
Thanks In Advance
推荐答案
嗨 Ganesh,
请使用以下代码:
Hi Ganesh,
Please use the following code:
Dim strMessage As String = ""
Public Const FILE_TYPE_NOT_ALLOWED As String = "|.exe|.ini|
Dim intFileSize As Decimal
Dim extension As String
If flu.HasFile Then
extension = "|" & Path.GetExtension(flu.FileName) & "|"
If FILE_TYPE_NOT_ALLOWED.Contains(extension) = True Then
strMessage = FILE_TYPE_NOT_ALLOWED_MESSAGE
Else
intFileSize = flu.PostedFile.ContentLength / 1048576
If intFileSize > SessionInfo.MaxRequestLength / 1024 Then
strMessage = String.Format(MAX_FILE_SIZE_MESSAGE, SessionInfo.MaxRequestLength / 1024)
Else
Dim strFile As String = flu.FileName
Dim strFolder As String = Path.Combine(FolderPath, getFolderType(Folder))
Dim GM As New GeneralMethod()
MakeDirectory(strFolder)
If File.Exists(HttpContext.Current.Server.MapPath(Path.Combine(strFolder, strFile))) Then
strMessage = GM.GetMsgHash("FileExists")
Else
flu.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(Path.Combine(strFolder, strFile)))
strMessage = GM.GetMsgHash("FileUploaded")
End If
End If
End If
End If
如有任何疑问,请让我知道.
如果对您有帮助,请提供投票.
谢谢,
Imdadhusen
Please do let me know, if you have any doubt.
Please provide Vote if this would be helpful to you.
Thanks,
Imdadhusen
Design:
<table>
<tr>
<td>
<asp:FileUpload ID="file1" runat="server" />
</table>
</tr>
</td>
Code:
//image Upload
string FileName = null;
HttpFileCollection uploadedFiles = Request.Files;
HttpPostedFile userPostedFile;
if (uploadedFiles.Count > 0)
{
userPostedFile = uploadedFiles[0];
if (userPostedFile.ContentLength > 0)
{
if (file1.PostedFile.ContentLength >= 262144)
{
ShowAlert("The file size should be less than 256KB");
}
else
{
FileName = System.IO.Path.GetFileName(file1.PostedFile.FileName);
int ind = FileName.LastIndexOf(".");
string fileType = FileName.Substring(ind + 1).ToString();
if (!((fileType == "jpg") || (fileType == "gif") || (fileType == "") || (fileType == "jpeg") || (fileType == "png") || (fileType == "PNG") || (fileType == "JPG") || (fileType == "GIF") || (fileType == "JPEG")))
{
ShowAlert("Please Upload only JPG/GIF/PNG image formats");
}
else
{
FileName = strEmployeeId + "_" + FileName;
string filepath = Server.MapPath("EmpImages");
string FileFullName = filepath + "\\" + FileName;
file1.PostedFile.SaveAs(FileFullName);
}
}
}
else
{
FileName = "";
}
}
{
userPostedFile = uploadedFiles[0];
if (userPostedFile.ContentLength > 0)
{
if (file1.PostedFile.ContentLength >= 262144)
{
ShowAlert("The file size should be less than 256KB");
}
else
{
FileName = System.IO.Path.GetFileName(file1.PostedFile.FileName);
int ind = FileName.LastIndexOf(".");
string fileType = FileName.Substring(ind + 1).ToString();
if (!((fileType == "jpg") || (fileType == "gif") || (fileType == "") || (fileType == "jpeg") || (fileType == "png") || (fileType == "PNG") || (fileType == "JPG") || (fileType == "GIF") || (fileType == "JPEG")))
{
ShowAlert("Please Upload only JPG/GIF/PNG image formats");
}
else
{
FileName = strEmployeeId + "_" + FileName;
string filepath = Server.MapPath("EmpImages");
string FileFullName = filepath + "\\" + FileName;
file1.PostedFile.SaveAs(FileFullName);
}
}
}
else
{
FileName = "";
}
}
这篇关于上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文