السلام عليكم ورحمة الله وبركاته
اخواني حصلت على مثال لرفع الملفات معمول بلغة c
وقمت بتحويله ل vb.net ولكن للاسف لم يعمل البرنامج معي
ولا اعرف اسن الخطأ
ارجو المساعدة لاني محتاجه ضروري
السلام عليكم ورحمة الله وبركاته
اخواني حصلت على مثال لرفع الملفات معمول بلغة c
وقمت بتحويله ل vb.net ولكن للاسف لم يعمل البرنامج معي
ولا اعرف اسن الخطأ
ارجو المساعدة لاني محتاجه ضروري
السلام عليكم ..
this.Session["UploadDetail"] = new UploadDetail { IsReady = false };تم تحويلها بشكل أعطت خطأ و تم تصحيحه.
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Data
Partial Public Class _Default
Inherits System.Web.UI.Page
#Region "Private Member Variables"
Private Shared UPLOADFOLDER As String = "Uploads"
#End Region
#Region "Web Methods"
Protected Sub Page_Load(ByVal sender As Object, ByVal args As EventArgs)
If Not Me.IsPostBack Then
'Reserve a spot in Session for the UploadDetail object
Dim uploadDetail As UploadDetail = New UploadDetail()
uploadDetail.IsReady = False
Me.Session("UploadDetail") = uploadDetail
LoadUploadedFiles(gvNewFiles)
End If
End Sub
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
Public Shared Function GetUploadStatus() As Object
'Get the length of the file on disk and divide that by the length of the stream
Dim info As UploadDetail = DirectCast(HttpContext.Current.Session("UploadDetail"), UploadDetail)
If info IsNot Nothing AndAlso info.IsReady Then
Dim soFar As Integer = info.UploadedLength
Dim total As Integer = info.ContentLength
Dim percentComplete As Integer = CInt(Math.Truncate(Math.Ceiling(CDbl(soFar) / CDbl(total) * 100)))
Dim message As String = "Uploading..."
Dim fileName As String = String.Format("{0}", info.FileName)
Dim downloadBytes As String = String.Format("{0} of {1} Bytes", soFar, total)
Return New With { _
Key .percentComplete = percentComplete, _
Key .message = message, _
Key .fileName = fileName, _
Key .downloadBytes = downloadBytes _
}
End If
'Not ready yet
Return Nothing
End Function
#End Region
#Region "Web Callbacks"
Protected Sub gvNewFiles_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onmouseover", "eventMouseOver(this)")
e.Row.Attributes.Add("onmouseout", "eventMouseOut(this)")
End If
End Sub
Protected Sub gvNewFiles_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
Select Case e.CommandName
Case "deleteFile"
DeleteFile(e.CommandArgument.ToString())
LoadUploadedFiles(gvNewFiles)
Exit Select
Case "downloadFile"
Dim strFolder As String = "Uploads"
Dim filePath As String = Path.Combine(strFolder, e.CommandArgument.ToString())
DownloadFile(filePath)
Exit Select
End Select
End Sub
Protected Sub hdRefereshGrid_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
LoadUploadedFiles(gvNewFiles)
End Sub
#End Region
#Region "Support Methods"
Public Sub LoadUploadedFiles(ByRef gv As GridView)
Dim dtFiles As DataTable = GetFilesInDirectory(HttpContext.Current.Server.MapPath(UPLOADFOLDER))
gv.DataSource = dtFiles
gv.DataBind()
If dtFiles IsNot Nothing AndAlso dtFiles.Rows.Count > 0 Then
Dim totalSize As Double = Convert.ToDouble(dtFiles.Compute("SUM(Size)", ""))
If totalSize > 0 Then
lblTotalSize.Text = CalculateFileSize(totalSize)
End If
End If
End Sub
Public Function GetFilesInDirectory(ByVal sourcePath As String) As DataTable
Dim dtFiles As New System.Data.DataTable()
If (Directory.Exists(sourcePath)) Then
dtFiles.Columns.Add(New System.Data.DataColumn("Name"))
dtFiles.Columns.Add(New System.Data.DataColumn("Size"))
dtFiles.Columns("Size").DataType = GetType(Double)
dtFiles.Columns.Add(New System.Data.DataColumn("ConvertedSize"))
Dim dir As New DirectoryInfo(sourcePath)
For Each files As FileInfo In dir.GetFiles("*.*")
Dim drFile As System.Data.DataRow = dtFiles.NewRow()
drFile("Name") = files.Name
drFile("Size") = files.Length
drFile("ConvertedSize") = CalculateFileSize(files.Length)
dtFiles.Rows.Add(drFile)
Next
End If
Return dtFiles
End Function
Public Sub DownloadFile(ByVal filePath As String)
If File.Exists(Server.MapPath(filePath)) Then
Dim strFileName As String = Path.GetFileName(filePath).Replace(" ", "%20")
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName)
Response.Clear()
Response.WriteFile(Server.MapPath(filePath))
Response.[End]()
End If
End Sub
Public Function DeleteFile(ByVal FileName As String) As String
Dim strMessage As String = ""
Try
Dim strPath As String = Path.Combine(UPLOADFOLDER, FileName)
If File.Exists(Server.MapPath(strPath)) = True Then
File.Delete(Server.MapPath(strPath))
strMessage = "File Deleted"
Else
strMessage = "File Not Found"
End If
Catch ex As Exception
strMessage = ex.Message
End Try
Return strMessage
End Function
Public Function CalculateFileSize(ByVal FileInBytes As Double) As String
Dim strSize As String = "00"
If FileInBytes < 1024 Then
strSize = FileInBytes & " B"
'Byte
ElseIf FileInBytes > 1024 And FileInBytes < 1048576 Then
strSize = Math.Round((FileInBytes / 1024), 2) & " KB"
'Kilobyte
ElseIf FileInBytes > 1048576 And FileInBytes < 107341824 Then
strSize = Math.Round((FileInBytes / 1024) / 1024, 2) & " MB"
'Megabyte
ElseIf FileInBytes > 107341824 And FileInBytes < 1099511627776L Then
strSize = Math.Round(((FileInBytes / 1024) / 1024) / 1024, 2) & " GB"
Else
'Gigabyte
strSize = Math.Round((((FileInBytes / 1024) / 1024) / 1024) / 1024, 2) & " TB"
End If
'Terabyte
Return strSize
End Function
#End Region
End Classتم تعديل هذه المشاركة بواسطة houssam11350_11350 في 20 ديسمبر 2011 في 10:41
لا إله إلا الله ... محمد رسول الله
لو كانت مشاركتي مفيدة و تريد تشجيعي على المزيد من العطاء , فضلا قم بتقييم المشاركة
المعرًف القديم : houssam11350_11350
من مواضيعي : ArabGenCode : مولد كود و إجراءات مخزنة و واجهات لجداول سيكوال سيرفر
اخي العزيز حسام
حاولت اجرب الكود ولكن لم يعمل معي
فلو تكرمت ان ترسل لي البرنامج بعد التعديل
ولك كل الشكر والعرفان
السلام عليكم ..
لا إله إلا الله ... محمد رسول الله
لو كانت مشاركتي مفيدة و تريد تشجيعي على المزيد من العطاء , فضلا قم بتقييم المشاركة
المعرًف القديم : houssam11350_11350
من مواضيعي : ArabGenCode : مولد كود و إجراءات مخزنة و واجهات لجداول سيكوال سيرفر