实现下载的代码?请指教

 因为现在要用到下载文件的实现,昨天晚上回去想了一下,那不就是把文件读出到IO流中,然后再从流中写入另外一个文件吗?

/// <summary>
    /// 下载文件
    /// </summary>
    private void Down()
    {
        System.IO.Stream stream = null;
        FileStream fs=null;
        byte[] buffer = new byte[10000];
        int length = 0; //文件长度
        long dataToRead; //将要读取的数据
        string filepath = @"D:/GJUN/项目/数据表.doc";
        string filename = System.IO.Path.GetFileName(filepath);
        try
        {
            stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
            fs = new FileStream(Server.MapPath("down.doc"), FileMode.Create);
            dataToRead = stream.Length;
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Discription", "attachment filename=" + filename);
            //读取文件
            while (dataToRead > 0)
            {
                if (Response.IsClientConnected)
                {
                    length = stream.Read(buffer, 0, 10000);
                    //将数据写到当前输出流
                    //Response.OutputStream.Write(buffer, 0, length);
                    fs.Write(buffer, 0, length);
                    fs.Flush();
                    //Response.Flush();
                    buffer = new byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    dataToRead = -1;
                }
            }
           
          
        }
        catch (Exception ex)
        {
            Response.Write("Error :" + ex.Message);
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
                fs.Close();
                fs.Dispose();
                stream.Dispose();
            }
        }

于是调试了一下就可以实现了,狂喜!

关机----准备看一下电视。刚一站起来,突然发觉被电脑骗了,更加被我自己给骗了。就站在那里想了N分钟,越想越不对噢!那并没有实现到下载啊,只是把一个文件COPY到了另外一个地方,而且换了个名字而已。那并不是真的从SERVER端下载文件到了CLIENT端啊。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个停车场模拟管理程序,可以采用队列数据结构来管理停车场内的车辆。以下是简单的 C 代码实现: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_SPACE 10 // 停车场最大容量 typedef struct car { char license[10]; // 车牌号 struct car *next; // 指向下一节点的指针 } Car; Car *head = NULL; // 停车场队列头指针 Car *tail = NULL; // 停车场队列尾指针 int count = 0; // 当前停车场内车辆数量 // 进入停车场 void in() { if (count >= MAX_SPACE) { printf("停车场已满,无法进入!\n"); return; } char license[10]; printf("输入车牌号:"); scanf("%s", license); Car *car = (Car*)malloc(sizeof(Car)); strcpy(car->license, license); car->next = NULL; if (head == NULL) { head = car; } else { tail->next = car; } tail = car; count++; printf("车辆 %s 进入停车场,当前车辆数:%d\n", license, count); } // 离开停车场 void out() { if (count <= 0) { printf("停车场内无车辆!\n"); return; } char license[10]; printf("输入车牌号:"); scanf("%s", license); Car *prev = NULL; Car *cur = head; while (cur != NULL) { if (strcmp(cur->license, license) == 0) { if (cur == head) { head = cur->next; } else if (cur == tail) { tail = prev; } else { prev->next = cur->next; } free(cur); count--; printf("车辆 %s 离开停车场,当前车辆数:%d\n", license, count); return; } prev = cur; cur = cur->next; } printf("停车场内无此车辆!\n"); } // 显示停车场内车辆信息 void show() { if (count <= 0) { printf("停车场内无车辆!\n"); return; } printf("停车场内车辆信息如下:\n"); Car *cur = head; while (cur != NULL) { printf("%s\n", cur->license); cur = cur->next; } } int main() { while (true) { printf("选择操作:\n"); printf("1. 进入停车场\n"); printf("2. 离开停车场\n"); printf("3. 显示停车场内车辆信息\n"); printf("4. 退出程序\n"); int choice; scanf("%d", &choice); switch (choice) { case 1: in(); break; case 2: out(); break; case 3: show(); break; case 4: return 0; default: printf("输入有误,重新选择操作!\n"); break; } } } ``` 该程序采用了简单的命令行交互方式,可以通过菜单选择进入停车场、离开停车场或查看停车场内车辆信息。停车场内车辆数量达到最大容量时,无法再进入车辆;停车场内无车辆时,无法离开车辆或查看车辆信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值