繁体   English   中英

使用合适的内容类型将图像上传到ASP.NET网站

[英]Uploading image to ASP.NET site with avalible content-type

我目前已经设置了网络服务,使用以下代码将文件(.png图像)上传并保存到我的网站:

[WebMethod]
public string SaveDocument(byte[] docbinaryarray, string docname)
{
    string strdocPath;
    string docDirPath = Server.MapPath("\\") + uploadDir;
    strdocPath = docDirPath + docname;
    if (!Directory.Exists(docDirPath))
        Directory.CreateDirectory(docDirPath);
    FileStream objfilestream = new FileStream(strdocPath, FileMode.Create, FileAccess.ReadWrite);
    objfilestream.Write(docbinaryarray, 0, docbinaryarray.Length);
    objfilestream.Close();
    return "http:\\\\example.com\\" + uploadDir + docname;    
}

这段代码可以正常工作,但是当我通过url访问图像时,没有得到与png文件(image / png)匹配的内容类型。 上传文件时是否可以关联内容类型,以便在访问URL(example.com/myimage.png)时返回具有image / png内容类型的图像? 还是我需要以某种方式动态创建具有链接到这些链接的内容类型的网页?

编辑:目标是将其与需要执行GET请求并需要知道内容类型的第三方API一起使用,现在它指定为无效。

我认为您的阻止文件的Web服务器中的配置不正确,或者您将错误的文件名传递给此Web服务方法。

我对其进行了测试并编写了简单的实现,并且一切正常。 ASP.NET页面将图像上传到Web服务,Web服务返回URL到上传的图像,页面通过Image1控件显示图像。

我的简单实现:

Default.aspx:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick" />
    <asp:Image ID="Image1" runat="server" />
    </form>
</body>
</html>

Default.aspx.cs:

namespace TestAspNetWebApp
{
    using System.IO;
    using System;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_OnClick(object sender, EventArgs e)
        {
            string appPath = Request.PhysicalApplicationPath + "\\uploads\\";

            if (!Directory.Exists(appPath))
                Directory.CreateDirectory(appPath);

            if (FileUpload1.HasFile)
            {
                string savePath = appPath + Server.HtmlEncode(FileUpload1.FileName);
                FileUpload1.SaveAs(savePath);

                using (var objfilestream = new FileStream(savePath, FileMode.Open, FileAccess.Read))
                {
                    var len = (int) objfilestream.Length;
                    var mybytearray = new Byte[len];
                    objfilestream.Read(mybytearray, 0, len);

                    var myservice = new WebService();
                    var fileurl = myservice.SaveDocument(mybytearray, FileUpload1.FileName);
                    Image1.ImageUrl = fileurl;
                }

                File.Delete(savePath);
            }
        }
    }
}

WebService.asmx.cs:

namespace TestAspNetWebApp
{
    using System.IO;
    using System.Web.Services;

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class WebService : System.Web.Services.WebService
    {
        private const string uploadDir = "uploads-websevice";

        [WebMethod]
        public string SaveDocument(byte[] docbinaryarray, string docname)
        {
            string strdocPath;

            string docDirPath = Server.MapPath("\\") + uploadDir + "\\";
            strdocPath = docDirPath + docname;

            if (!Directory.Exists(docDirPath))
                Directory.CreateDirectory(docDirPath);

            var objfilestream = new FileStream(strdocPath, FileMode.Create, FileAccess.ReadWrite);
            objfilestream.Write(docbinaryarray, 0, docbinaryarray.Length);
            objfilestream.Close();

            return "http://localhost:57064/" + uploadDir + "/" + docname;
        }
    }
}

请添加一些代码示例,以帮助您定义问题并缓解问题。

似乎服务器照常返回所有标头以及“ Content-type”。

using (var client = new WebClient())
{
      string uri = "http://chronicletwilio.azurewebsites.net/res/12345.png";
      byte[] data = client.DownloadData(uri);
      string contentType = client.ResponseHeaders["content-type"];
      // contentType is 'image/png'
}

我解决了这个问题。 即使设置了内容类型标头,该文件也不会作为图像读取,而只是作为文件读取。 在这种情况下,我使用的第3方API无法正确读取它并抱怨内容类型。 该修复程序最终将我的方法更改为以下内容:

[WebMethod]
        public string SaveImage(byte[] docbinaryarray, string docname)
        {
            string strdocPath;
            string docDirPath = Server.MapPath("/") + uploadDir;
            strdocPath = docDirPath + docname;
            if (!Directory.Exists(docDirPath))
                Directory.CreateDirectory(docDirPath);
            using (Image image = Image.FromStream(new MemoryStream(docbinaryarray)))
            {
                image.Save(strdocPath, ImageFormat.Png);  // Or Png
            }
            return "http://example.com/" + uploadDir + docname;

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM