簡體   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