c# - SaveAs方法配置为需要root路径,而路径 '~/Images/FleaMarket/uploadedImages/mitali2054/5.jpg'不是root路径

标签 c# asp.net vb.net

Private Function UploadPic() As String

        Const bmpw As Integer = 300
        Const bmph As Integer = 300
        Dim itemID As String = MaxItemNo()

        Dim filePath As String = "~/Images/FleaMarket/uploadedImages/" & User.Identity.Name.ToString & "/" & itemID & Path.GetExtension(fuImage.FileName)
        MsgBox(filePath)
        If fuImage.HasFile Then
            'lblMessage.Text = ""
            If checkFileType(fuImage.FileName) Then
                **fuImage.PostedFile.SaveAs(filePath)**
                Dim newWidth As Integer = bmpw
                Dim newHeight As Integer = bmph
                Dim upBmp As Bitmap = Bitmap.FromStream(fuImage.PostedFile.InputStream)
                Dim newBmp As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb)
                newBmp.SetResolution(72, 72)
                Dim upWidth As Integer = upBmp.Width
                Dim upHeight As Integer = upBmp.Height
                Dim newX As Integer = 0
                Dim newY As Integer = 0
                Dim reduce As Decimal

                If upWidth > upHeight Then
                    reduce = newWidth / upWidth
                    newHeight = Int(upHeight * reduce)
                    newY = Int((bmph - newHeight) / 2)
                    newX = 0
                ElseIf upWidth < upHeight Then
                    reduce = newHeight / upHeight
                    newWidth = Int(upWidth * reduce)
                    newX = Int((bmpw - newWidth) / 2)
                    newY = 0
                ElseIf upWidth = upHeight Then
                    reduce = newHeight / upHeight
                    newWidth = Int(upWidth * reduce)
                    newX = Int((bmpw - newWidth) / 2)
                    newY = Int((bmph - newHeight) / 2)
                End If

                Dim newGraphic As Graphics = Graphics.FromImage(newBmp)
                Try
                    newGraphic.Clear(Color.White)
                    newGraphic.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                    newGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                    newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight)
                    newBmp.Save(MapPath(filePath), Imaging.ImageFormat.Png)
                    'Image1.ImageUrl = filePath
                    'Image1.Visible = True
                Catch ex As Exception
                    'lblMessage.Text = ex.ToString
                Finally
                    upBmp.Dispose()
                    newBmp.Dispose()
                    newGraphic.Dispose()
                End Try

            Else
                lblMessage.Text = "Please select bmp, jpg, jpeg, gif or png as file format!"
            End If
        End If
        Return filePath
    End Function

这是我用来上传图片的代码....但我收到错误”SaveAs 方法配置为需要根路径,并且路径'~/Images/FleaMarket/uploadedImages/mitali2054/5.jpg'未root。”

最佳答案

Server.MapPath 应配置为在服务器端写入磁盘:

基本用法:

String FilePath = Server.MapPath("/App_Data");

配置:可以引用这两个Q&A

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

The SaveAs method is configured to require a rooted path, and the path 'fp' is not rooted

关于c# - SaveAs方法配置为需要root路径,而路径 '~/Images/FleaMarket/uploadedImages/mitali2054/5.jpg'不是root路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7864586/

相关文章:

c# - 我应该如何从 P&P 为 EventAggregator 创建事件,以便 UI 线程上的订阅者可以收听它们?

c# - 抽象类的访问修饰符

asp.net - 无法为 Elmah 配置邮件

asp.net - 在操作之外执行异步操作 asp.net mvc

wpf - 正确的 MVVM 模式 WPF 命令实现

c# - 如何从一个类调用另一个类的方法

c# - 具有日期范围的数据表过滤器

c# - 返回零的随机数生成器

mysql - 如何在 firebird 中插入另一个表中的行?

java - 我有可用的 JAVA 和 VB 代码。我需要用 PHP 做同样的事情,我该怎么做