iT邦幫忙

0

關於把檔案上傳至sqlserver問題

是這樣的,我今天需要一個小功能,是把檔案上傳至sqlserver裡,
我後來有找到參考網站,我是參考這個
http://www.dotnetawesome.com/2013/11/how-to-upload-and-download-files-tofrom.html
只是為什麼我打完後,用chrome開啟,無法上傳,網頁直接顯示localhost 拒絕連線。
但用edge就可以上傳,但也只能上傳一個,若上傳一個以上他就會顯示這種錯誤
https://ithelp.ithome.com.tw/upload/images/20180712/20110132p18Ew4zjhq.jpg

以下是我的程式碼
前端:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h3>File Upload / Download from/to Database using ASP.NET</h3>
    <div>
        <table>
            <tr>
               <td>select File:</td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" /></td>
                <td>
                    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click1" /></td>
            </tr>
        </table>
        <br />
        <div>
            <asp:DataList ID="DataList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td><%#Eval("FileName","File Name:{0}") %></td>
                        </tr>
                        <tr>
                            <td><%# Convert.ToDecimal(Eval("Filesize"))/1024%> KB</td>
                        </tr>
                        <tr>
                            <td>
                                <asp:LinkButton ID="lbtnDownload" runat="server" CommandName="Download" CommandArgument=<%# Eval("FileID") %>>Download</asp:LinkButton></td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
        </div>
    </div>

</asp:Content>

後端部分:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateUploadedFiles();
            }
        }

        private void PopulateUploadedFiles()
        {
            using (UpLoadDatabase1Entities uc = new UpLoadDatabase1Entities())
            {
                List<UploadedFile> allFiles = uc.UploadedFiles.ToList();
                DataList1.DataSource = allFiles;
                DataList1.DataBind();
            }
        }

        protected void btnUpload_Click1(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                HttpPostedFile file = FileUpload1.PostedFile;
                BinaryReader br = new BinaryReader(file.InputStream);
                byte[] buffer = br.ReadBytes(file.ContentLength);

                using (UpLoadDatabase1Entities uc = new UpLoadDatabase1Entities())
                {
                    uc.UploadedFiles.Add(
                        new UploadedFile
                        {
                            FileName =file.FileName,
                            ContentType=file.ContentType,
                            FileID=0,
                            FileSize=file.ContentLength,
                            FileExtension=Path.GetExtension(file.FileName),
                            FileContent = buffer
                        });
                    uc.SaveChanges();
                    PopulateUploadedFiles();
                }
            }
        }
    }
}

資料庫:

CREATE TABLE [dbo].[UploadedFile] (
    [FileID]        INT             NOT NULL,
    [FileName]      VARCHAR (200)   NOT NULL,
    [FileSize]      INT             NOT NULL,
    [ContentType]   VARCHAR (200)   NOT NULL,
    [FileExtension] VARCHAR (10)    NOT NULL,
    [FileContent]   VARBINARY (MAX) NOT NULL,
    PRIMARY KEY CLUSTERED ([FileID] ASC)
);

目前暫時找不到問題所在,還請各位為我解答,謝謝。

看更多先前的討論...收起先前的討論...
優悠 iT邦新手 3 級 ‧ 2018-07-12 17:41:01 檢舉
CREATE TABLE 時 主見要多這一段
identity(1,1) unique
優悠 iT邦新手 3 級 ‧ 2018-07-12 17:41:52 檢舉
你 new UploadedFile 不需要FileID=0
優悠 iT邦新手 3 級 ‧ 2018-07-12 17:42:33 檢舉
給主見=0,就重複無法新增
tenno081 iT邦研究生 4 級 ‧ 2018-07-12 18:53:51 檢舉
你好。我在我的主鍵加入您說的那段然後FileID設為1之後
結果變成Cannot insert explicit value for identity column in table 'UploadedFile' when IDENTITY_INSERT is set to OFF.
後來我把UploadedFile開啟後,還是錯誤,最後我把我的主鍵取消掉跟識別規格
取消掉之後,又回到原點。請問我該如何處理?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2018-07-13 08:16:34
最佳解答

依據你參考的文章
資料表的部份改成如下

CREATE TABLE [dbo].[UploadedFile] (
    [FileID]        INT             NOT NULL,
    [FileName]      VARCHAR (200)   NOT NULL,
    [FileSize]      INT             NOT NULL,
    [ContentType]   VARCHAR (200)   NOT NULL,
    [FileExtension] VARCHAR (10)    NOT NULL,
    [FileContent]   VARBINARY (MAX) NOT NULL
);
看更多先前的回應...收起先前的回應...
tenno081 iT邦研究生 4 級 ‧ 2018-07-13 08:51:18 檢舉

可以了,太感謝您了,只是我這個功能還是無法在chrome上執行,
想請問可能原因是?

w4560000 iT邦研究生 5 級 ‧ 2018-07-13 09:06:22 檢舉

原先你是把FileID 設成主鍵,又固定塞0給他,重複給值才會出錯。
現在主鍵拿掉,你顯示的錯誤有改變嗎?

試試看把啟用編輯後繼續點掉

tenno081 iT邦研究生 4 級 ‧ 2018-07-13 09:59:59 檢舉

可以了,十分感謝。

tenno081 iT邦研究生 4 級 ‧ 2018-07-13 11:04:00 檢舉

你好,能否在請問,我有找到說可以把檔案上傳到webserver硬碟,
然後把路徑傳至database裡,能否大略告訴我一下大概要怎麼做,
我想拿來做個比較看看,我這個是參考來的,但我覺得不是很好懂。

我沒什麼好說
還是參考高手的文章好些

tenno081 iT邦研究生 4 級 ‧ 2018-07-13 13:11:10 檢舉

這個我有看過,只是我不太懂他說的Web Server的硬碟這個是?

我的理解是 圖片在server內的路徑

我要發表回答

立即登入回答